Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EntityContext creates not valid children entities if parent has no children #5190

Closed
MichalWadowski opened this issue Nov 17, 2014 · 7 comments

Comments

@MichalWadowski
Copy link
Contributor

I have Offer table associated to Customers with relation belongsToMany() (codes below are simplified for better reability):

public function initialize(array $config) {
   $this->belongsToMany("Customers", [
      'through' => 'OffersCustomers',
      'foreignKey' => 'offer_id',
   ]);
}

In CustomersTable.php:

public function validationDefault(Validator $validator) {
   $validator->notEmpty('company_name', function ($context) {
   return $context['providers']['entity']->isCompany();
});

I have add() controller action:

   public function add() {
   $offer = $this->Offers->newEntity($this->request->data, [
      'associated' => [
         'Customers'
      ]
   ]);
   $this->set('offer', $offer);
}

And view:

<?php echo $this->Form->create($offer); ?>
<?php echo $this->Form->input('customers.0.first_name'); ?>
<?php echo $this->Form->end(); ?>

Now, I can't display view, because I have unknown method error (isCompany() method not found). In CustomersTable in validation callback in $context['providers']['entity'] is stored Cake\ORM\Entity object, instead of App\Model\Entity\Customer.
In EntityContext.php method _getEntity($path) creates an entity by simple new Entity().
But if in POST data $this->request->data I have something like:

[
    'customers' => [
        (int) 0 => [
            'first_name' => 'dsa'
        ]
    ]
]

in $context['providers']['entity'] is stored App\Model\Entity\Customer object.

@lorenzo
Copy link
Member

lorenzo commented Nov 17, 2014

This features has a very high chance of being removed, once the validation code is reworked. This might influence our decision in what to do about this issue.

@markstory
Copy link
Member

One solution that should work here is that instead of the base entity class being used by _getEntity() the respective table's newEntity() method should probably be used instead. I think this will solve the issue here.

@saraer
Copy link

saraer commented Nov 18, 2014

same problem here.
I can not use entity method after post, like in validation error situation, because EntityContext::val() returns request->data(just array) before $entity check. I think if form is set by $entity data, EntityContext should respect $entity than request->data.

@markstory
Copy link
Member

The reason it prefers request data is that there can be situations where request data does not get added into the entity. If I'm understanding you correctly the lookup order should be:

  • Entity attribute
  • If the field is not set on the entity, then check the request data.
  • null.

@markstory markstory self-assigned this Nov 18, 2014
markstory added a commit that referenced this issue Nov 19, 2014
markstory added a commit that referenced this issue Nov 19, 2014
When an association is undefined return a concrete entity class. This is
necessary as notEmpty/allowEmpty callbacks could reference the entity
provider.

Refs #5190
@markstory
Copy link
Member

@saraer How would I reproduce the issue you're running into. I'm not sure I fully understand it.

@saraer
Copy link

saraer commented Nov 25, 2014

@markstory My problem was cleared by not using method in view. I think my problem is caused by my own coding way.
After all, I think lookup order should be entity attribute is first, but even if not, that'll be not maybe any problem. when I can find any another decent problem of this, I will post issue again.

@markstory
Copy link
Member

Ok. I'll close this issue for now, and we can discuss further when a new issue comes up 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants