That was a clue! 

The problem was that I was calling patchEntity() using only the 
customer_id. patchEntity validates the stuff you are "patching" with 
instead of the resulting entity so, it flagged errors on the missing 
values. IMO that is unexpected behavior, but it can be remedied with 
['validate'=> false].


On Monday, February 16, 2015 at 5:30:09 PM UTC-5, Harold Putman wrote:
>
> No sorry, I was retyping and my eyesight is bad ;-)
>
> What I am finding as I dig deeper is that somehow I am getting errors in 
> the user Entity that username and password fields are required even though 
> they are set:
>  
> object(App\Model\Entity\User) {
> 'new' => true,
> 'accessible' => [
> 'username' => true,
> 'password' => true,
> 'customer_id' => true
> ],
> 'properties' => [
> customer_id' => (int) 1002,
> 'username' => '[email protected]',
> 'password' => 'password'
> ],
> 'dirty' => [
> 'customer_id' => true,
> 'username' => true,
> 'password' => true
> ],
> 'original' => [
> 'customer_id' => (int) 1003
> ],
> 'virtual' => [],
> 'errors' => [
> 'username' => [
> '_required' => 'This field is required'
> ],
> 'password' => [
> '_required' => 'This field is required'
> ]
> ],
> 'repository' => 'Users'
> }
>
> I'm sure this is a clue...
>
>
> On Monday, February 16, 2015 at 4:33:38 PM UTC-5, Andrew Lechowicz wrote:
>>
>> Is that directly copied/pasted? If so, do you mean to have a 'Users=>
>> patchEntity' in this line: 
>> $user = $this->Customers->Users=>patchEntity($user, ['customer_id' => $cr
>> ->get('id')]);
>> Notice the double arrow (=>)?
>>
>>
>> On Monday, February 16, 2015 at 6:45:40 AM UTC-5, Harold Putman wrote:
>>>
>>> Hi,
>>> I am having trouble creating new records that are in a BelongsTo 
>>> association
>>>
>>> Two tables: Users and Customers. User = [id, username, customer_id] 
>>>  Customer = [id, firstname, lastname]
>>>
>>> I have a form where someone can enter username, firstname, and lastname. 
>>> I want to create a new Customer record and related User record.
>>>
>>> Starting from the baked code in CustomersController 'add' function I have
>>>
>>> ```
>>>         $customer = $this->Customers->newEntity();
>>>         if ($this->request->is('post')) {
>>>             $customer = $this->Customers->patchEntity($customer, 
>>> $this->request->data);
>>>             $cr = $this->Customers->save($customer)
>>>             if ($cr) {
>>>                 // Trying to create User:
>>>                 $user = $this->Customers->Users->newEntity();
>>>                 $user = $this->Customers->Users->patchEntity($user, 
>>> $this->request->data);
>>>                 $user = $this->Customers->Users=>patchEntity($user, 
>>> ['customer_id' => $cr->get('id')]);
>>>                 if ($this->Customers->Users->save($user)) {
>>>                   // save just returns false. 
>>>                    $this->Flash->success('The customer has been saved.');
>>>                    return $this->redirect(['action' => 'index']);
>>>                 }
>>>             } else {
>>>                 $this->Flash->error('The customer could not be saved. 
>>> Please, try again.');
>>>             }
>>>         }
>>> ```
>>>
>>> I've checked for validation errors and there are none. Am I going about 
>>> this wrong?? 
>>>
>>> This is the tail of the SQL log with or without the "save()" call. It's 
>>> like save is not even tried.  Any ideas what is going on here?
>>>
>>> SHOW FULL COLUMNS FROM `users`61SHOW INDEXES FROM `users`20SELECT * 
>>> FROM information_schema.key_column_usage AS kcu INNER JOIN 
>>> information_schema.referential_constraints AS rc ON (kcu.CONSTRAINT_NAME = 
>>> rc.CONSTRAINT_NAME) WHERE kcu.TABLE_SCHEMA = 'hotdogs_data' AND 
>>> kcu.TABLE_NAME = 'users' and rc.TABLE_NAME = 'users'00SHOW TABLE STATUS 
>>> WHERE Name = 'users'
>>>
>>

-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

--- 
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

Reply via email to