Hi,
I am creating a user edit form consisting of five related entities as follows:
Buyers belongsTo Users
Genres belongsToMany Buyers
Buyers belongsToMany Genres
Users hasOne Buyers
Users hasMany Addresses
Addresses belongsTo Users
Addresses belongsTo Countries
Countries hasMany Addresses
I create Buyers entity and sending the data to the view as follows:
$Buyer = $this->Buyers-> get($id ['contain' => ['Users', 'Users.Addresses', 'Users.Addresses.Countries', 'Genres']]);
Then send to the edit.ctp:
$Countries = $this->Buyers->Users->Addresses->Countries-> find('list');
$Genres = $this->Buyers->Genres->find('list' ['order' => ['description_pt' => 'ASC']]);
$This->set(compact ('buyer', 'countries', 'genres'));
$This->set( '_serialize'['buyer', 'countries', 'genres']);
The form data is correctly presented, which shows that the cake recognized all fields as are in the database and they are filled with the correct data.
When sending the data they appear so (using debug):
object(App\Model\Entity\Buyer) {
'new' => false,
'accessible' => [
'cpf' => true,
'gender' => true,
'birthdate' => true,
'newsletter' => true,
'user_id' => true,
'user' => true,
'genres' => true
],
'properties' => [
'id' => (int) 68,
'cpf' => '01847348785',
'gender' => 'Masculino',
'birthdate' => object(Cake\I18n\Time) {
'time' => '1971-03-23T00:00:00+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'newsletter' => 'Sim',
'user_id' => (int) 164,
'genres' => [
(int) 0 => object(App\Model\Entity\Genre) {
'new' => false,
'accessible' => [
'description_pt' => true,
'description_en' => true,
'description_es' => true,
'buyers' => true,
'records' => true
],
'properties' => [
'id' => (int) 3,
'description_pt' => 'Bossa-Nova',
'description_en' => 'Bossa-Nova',
'description_es' => 'Bossa-Nova',
'_joinData' => object(App\Model\Entity\BuyersGenre) {
'new' => false,
'accessible' => [
'genre_id' => true,
'buyer_id' => true,
'genre' => true,
'buyer' => true
],
'properties' => [
'genre_id' => (int) 3,
'id' => (int) 66,
'buyer_id' => (int) 68
],
'dirty' => [],
'original' => [],
'virtual' => [],
'errors' => [],
'repository' => 'BuyersGenres'
}
],
'dirty' => [],
'original' => [],
'virtual' => [],
'errors' => [],
'repository' => 'Genres'
}
],
'user' => object(App\Model\Entity\User) {
'new' => false,
'accessible' => [
'name' => true,
'password' => true,
'username' => true,
'status' => true,
'session' => true,
'ip' => true,
'role' => true,
'login_error' => true,
'survey_id' => true,
'survey' => true,
'other' => true,
'addresses' => true,
'buyers' => true,
'carts' => true,
'classifieds' => true,
'last_visited_ids' => true,
'orders' => true,
'refer_friends' => true,
'searches' => true,
'sellers' => true
],
'properties' => [
'id' => (int) 164,
'name' => 'Paulo Terra Matias',
'password' => '$2y$10$JKRUBVNjOon9amTnvKvrDeU4D/ZhVvYpKUDv9op1lC4eRIcP4es8G',
'username' => 'pterra@gmail.com',
'created' => object(Cake\I18n\Time) {
'time' => '2016-04-09T21:07:28+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'modified' => object(Cake\I18n\Time) {
'time' => '2016-04-17T21:26:16+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'status' => 'Ativo',
'session' => 'd0a500904fd63a39ccb6d7556210ecfb',
'ip' => '201.53.143.193',
'role' => 'Comprador',
'login_error' => (int) 0,
'survey_id' => (int) 6,
'other' => 'sadasdsa',
'addresses' => [
(int) 0 => object(App\Model\Entity\Address) {
'new' => false,
'accessible' => [
'user_id' => true,
'country_id' => true,
'code' => true,
'type' => true,
'zipcode' => true,
'line1' => true,
'line2' => true,
'city' => true,
'state' => true,
'country' => true,
'phone' => true,
'celphone' => true,
'user' => true
],
'properties' => [
'id' => (int) 29,
'user_id' => (int) 164,
'code' => (int) 1,
'type' => 'Principal',
'zipcode' => '22222222',
'line1' => 'kjhkj',
'line2' => 'jhk',
'city' => 'jhkjhkj',
'state' => 'hj',
'country_id' => (int) 81,
'phone' => '',
'celphone' => '',
'country' => object(App\Model\Entity\Country) {
'new' => false,
'accessible' => [
'country_name' => true,
'addresses' => true
],
'properties' => [
'id' => (int) 81,
'country_name' => 'Haiti'
],
'dirty' => [],
'original' => [],
'virtual' => [],
'errors' => [],
'repository' => 'Countries'
}
],
'dirty' => [],
'original' => [],
'virtual' => [],
'errors' => [],
'repository' => 'Addresses'
}
]
],
'dirty' => [],
'original' => [],
'virtual' => [],
'errors' => [],
'repository' => 'Users'
}
],
'dirty' => [],
'original' => [],
'virtual' => [],
'errors' => [],
'repository' => 'Buyers'
}
The data that comes from the form after the post:
[
'user' => [
'name' => 'Paulo Terra Matias',
'username' => 'pterra@gmail.com',
'addresses' => [
(int) 0 => [
'phone' => '999999999',
'celphone' => '',
'zipcode' => '22222222',
'line1' => 'kjhkj',
'line2' => 'jhk',
'city' => 'jhkjhkj',
'state' => 'hj',
'country_id' => '81'
]
]
],
'cpf' => '01847348785',
'gender' => 'Masculino',
'birthdate' => [
'year' => '1971',
'month' => '03',
'day' => '23'
],
'genres' => [
'_ids' => [
(int) 0 => '3'
]
],
'newsletter' => 'Sim'
]
==========================================
However, after patchEntity the entity Address is marked as 'new' => true,
This makes the cake try to create a new Address record, when in fact, I want to edit the current Address record.
What am I doing wrong?
$ Buyer = $ this-> Buyers-> patchEntity ($ buyer, $ this-> request-> data [
'Associated' => [
'Users',
'Users.Addresses'
'Users.Addresses.Countries'
'Genres'
]
]);
-- I am creating a user edit form consisting of five related entities as follows:
Buyers belongsTo Users
Genres belongsToMany Buyers
Buyers belongsToMany Genres
Users hasOne Buyers
Users hasMany Addresses
Addresses belongsTo Users
Addresses belongsTo Countries
Countries hasMany Addresses
I create Buyers entity and sending the data to the view as follows:
$Buyer = $this->Buyers-> get($id ['contain' => ['Users', 'Users.Addresses', 'Users.Addresses.Countries', 'Genres']]);
Then send to the edit.ctp:
$Countries = $this->Buyers->Users->Addresses->Countries-> find('list');
$Genres = $this->Buyers->Genres->find('list' ['order' => ['description_pt' => 'ASC']]);
$This->set(compact ('buyer', 'countries', 'genres'));
$This->set( '_serialize'['buyer', 'countries', 'genres']);
The form data is correctly presented, which shows that the cake recognized all fields as are in the database and they are filled with the correct data.
When sending the data they appear so (using debug):
object(App\Model\Entity\Buyer) {
'new' => false,
'accessible' => [
'cpf' => true,
'gender' => true,
'birthdate' => true,
'newsletter' => true,
'user_id' => true,
'user' => true,
'genres' => true
],
'properties' => [
'id' => (int) 68,
'cpf' => '01847348785',
'gender' => 'Masculino',
'birthdate' => object(Cake\I18n\Time) {
'time' => '1971-03-23T00:00:00+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'newsletter' => 'Sim',
'user_id' => (int) 164,
'genres' => [
(int) 0 => object(App\Model\Entity\Genre) {
'new' => false,
'accessible' => [
'description_pt' => true,
'description_en' => true,
'description_es' => true,
'buyers' => true,
'records' => true
],
'properties' => [
'id' => (int) 3,
'description_pt' => 'Bossa-Nova',
'description_en' => 'Bossa-Nova',
'description_es' => 'Bossa-Nova',
'_joinData' => object(App\Model\Entity\BuyersGenre) {
'new' => false,
'accessible' => [
'genre_id' => true,
'buyer_id' => true,
'genre' => true,
'buyer' => true
],
'properties' => [
'genre_id' => (int) 3,
'id' => (int) 66,
'buyer_id' => (int) 68
],
'dirty' => [],
'original' => [],
'virtual' => [],
'errors' => [],
'repository' => 'BuyersGenres'
}
],
'dirty' => [],
'original' => [],
'virtual' => [],
'errors' => [],
'repository' => 'Genres'
}
],
'user' => object(App\Model\Entity\User) {
'new' => false,
'accessible' => [
'name' => true,
'password' => true,
'username' => true,
'status' => true,
'session' => true,
'ip' => true,
'role' => true,
'login_error' => true,
'survey_id' => true,
'survey' => true,
'other' => true,
'addresses' => true,
'buyers' => true,
'carts' => true,
'classifieds' => true,
'last_visited_ids' => true,
'orders' => true,
'refer_friends' => true,
'searches' => true,
'sellers' => true
],
'properties' => [
'id' => (int) 164,
'name' => 'Paulo Terra Matias',
'password' => '$2y$10$JKRUBVNjOon9amTnvKvrDeU4D/ZhVvYpKUDv9op1lC4eRIcP4es8G',
'username' => 'pterra@gmail.com',
'created' => object(Cake\I18n\Time) {
'time' => '2016-04-09T21:07:28+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'modified' => object(Cake\I18n\Time) {
'time' => '2016-04-17T21:26:16+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'status' => 'Ativo',
'session' => 'd0a500904fd63a39ccb6d7556210ecfb',
'ip' => '201.53.143.193',
'role' => 'Comprador',
'login_error' => (int) 0,
'survey_id' => (int) 6,
'other' => 'sadasdsa',
'addresses' => [
(int) 0 => object(App\Model\Entity\Address) {
'new' => false,
'accessible' => [
'user_id' => true,
'country_id' => true,
'code' => true,
'type' => true,
'zipcode' => true,
'line1' => true,
'line2' => true,
'city' => true,
'state' => true,
'country' => true,
'phone' => true,
'celphone' => true,
'user' => true
],
'properties' => [
'id' => (int) 29,
'user_id' => (int) 164,
'code' => (int) 1,
'type' => 'Principal',
'zipcode' => '22222222',
'line1' => 'kjhkj',
'line2' => 'jhk',
'city' => 'jhkjhkj',
'state' => 'hj',
'country_id' => (int) 81,
'phone' => '',
'celphone' => '',
'country' => object(App\Model\Entity\Country) {
'new' => false,
'accessible' => [
'country_name' => true,
'addresses' => true
],
'properties' => [
'id' => (int) 81,
'country_name' => 'Haiti'
],
'dirty' => [],
'original' => [],
'virtual' => [],
'errors' => [],
'repository' => 'Countries'
}
],
'dirty' => [],
'original' => [],
'virtual' => [],
'errors' => [],
'repository' => 'Addresses'
}
]
],
'dirty' => [],
'original' => [],
'virtual' => [],
'errors' => [],
'repository' => 'Users'
}
],
'dirty' => [],
'original' => [],
'virtual' => [],
'errors' => [],
'repository' => 'Buyers'
}
The data that comes from the form after the post:
[
'user' => [
'name' => 'Paulo Terra Matias',
'username' => 'pterra@gmail.com',
'addresses' => [
(int) 0 => [
'phone' => '999999999',
'celphone' => '',
'zipcode' => '22222222',
'line1' => 'kjhkj',
'line2' => 'jhk',
'city' => 'jhkjhkj',
'state' => 'hj',
'country_id' => '81'
]
]
],
'cpf' => '01847348785',
'gender' => 'Masculino',
'birthdate' => [
'year' => '1971',
'month' => '03',
'day' => '23'
],
'genres' => [
'_ids' => [
(int) 0 => '3'
]
],
'newsletter' => 'Sim'
]
==========================================
However, after patchEntity the entity Address is marked as 'new' => true,
This makes the cake try to create a new Address record, when in fact, I want to edit the current Address record.
What am I doing wrong?
$ Buyer = $ this-> Buyers-> patchEntity ($ buyer, $ this-> request-> data [
'Associated' => [
'Users',
'Users.Addresses'
'Users.Addresses.Countries'
'Genres'
]
]);
object(App\Model\Entity\Buyer) { 'new' => false, 'accessible' => [ 'cpf' => true, 'gender' => true, 'birthdate' => true, 'newsletter' => true, 'user_id' => true, 'user' => true, 'genres' => true ], 'properties' => [ 'id' => (int) 68, 'cpf' => '01847348785', 'gender' => 'Masculino', 'birthdate' => object(Cake\I18n\Time) { 'time' => '1971-03-23T00:00:00+0000', 'timezone' => 'UTC', 'fixedNowTime' => false }, 'newsletter' => 'Sim', 'user_id' => (int) 164, 'genres' => [ (int) 0 => object(App\Model\Entity\Genre) { 'new' => false, 'accessible' => [ 'description_pt' => true, 'description_en' => true, 'description_es' => true, 'buyers' => true, 'records' => true ], 'properties' => [ 'id' => (int) 3, 'description_pt' => 'Bossa-Nova', 'description_en' => 'Bossa-Nova', 'description_es' => 'Bossa-Nova' ], 'dirty' => [], 'original' => [], 'virtual' => [], 'errors' => [], 'repository' => 'Genres' } ], 'user' => object(App\Model\Entity\User) { 'new' => false, 'accessible' => [ 'name' => true, 'password' => true, 'username' => true, 'status' => true, 'session' => true, 'ip' => true, 'role' => true, 'login_error' => true, 'survey_id' => true, 'survey' => true, 'other' => true, 'addresses' => true, 'buyers' => true, 'carts' => true, 'classifieds' => true, 'last_visited_ids' => true, 'orders' => true, 'refer_friends' => true, 'searches' => true, 'sellers' => true ], 'properties' => [ 'id' => (int) 164, 'name' => 'Paulo Terra Matias', 'password' => '$2y$10$JKRUBVNjOon9amTnvKvrDeU4D/ZhVvYpKUDv9op1lC4eRIcP4es8G', 'username' => 'pterra@gmail.com', 'created' => object(Cake\I18n\Time) { 'time' => '2016-04-09T21:07:28+0000', 'timezone' => 'UTC', 'fixedNowTime' => false }, 'modified' => object(Cake\I18n\Time) { 'time' => '2016-04-17T21:26:16+0000', 'timezone' => 'UTC', 'fixedNowTime' => false }, 'status' => 'Ativo', 'session' => 'd0a500904fd63a39ccb6d7556210ecfb', 'ip' => '201.53.143.193', 'role' => 'Comprador', 'login_error' => (int) 0, 'survey_id' => (int) 6, 'other' => 'sadasdsa', 'addresses' => [ (int) 0 => object(App\Model\Entity\Address) { 'new' => true, 'accessible' => [ 'user_id' => true, 'country_id' => true, 'code' => true, 'type' => true, 'zipcode' => true, 'line1' => true, 'line2' => true, 'city' => true, 'state' => true, 'country' => true, 'phone' => true, 'celphone' => true, 'user' => true ], 'properties' => [ 'phone' => '999999999', 'celphone' => '', 'zipcode' => '22222222', 'line1' => 'kjhkj', 'line2' => 'jhk', 'city' => 'jhkjhkj', 'state' => 'hj', 'country_id' => (int) 81 ], 'dirty' => [ 'phone' => true, 'celphone' => true, 'zipcode' => true, 'line1' => true, 'line2' => true, 'city' => true, 'state' => true, 'country_id' => true ], 'original' => [], 'virtual' => [], 'errors' => [ 'code' => [ '_required' => 'This field is required' ], 'type' => [ '_required' => 'This field is required' ] ], 'repository' => 'Addresses' } ] ], 'dirty' => [ 'addresses' => true ], 'original' => [ 'addresses' => [ (int) 0 => object(App\Model\Entity\Address) { 'new' => false, 'accessible' => [ 'user_id' => true, 'country_id' => true, 'code' => true, 'type' => true, 'zipcode' => true, 'line1' => true, 'line2' => true, 'city' => true, 'state' => true, 'country' => true, 'phone' => true, 'celphone' => true, 'user' => true ], 'properties' => [ 'id' => (int) 29, 'user_id' => (int) 164, 'code' => (int) 1, 'type' => 'Principal', 'zipcode' => '22222222', 'line1' => 'kjhkj', 'line2' => 'jhk', 'city' => 'jhkjhkj', 'state' => 'hj', 'country_id' => (int) 81, 'phone' => '', 'celphone' => '', 'country' => object(App\Model\Entity\Country) { 'new' => false, 'accessible' => [ 'country_name' => true, 'addresses' => true ], 'properties' => [ 'id' => (int) 81, 'country_name' => 'Haiti' ], 'dirty' => [], 'original' => [], 'virtual' => [], 'errors' => [], 'repository' => 'Countries' } ], 'dirty' => [], 'original' => [], 'virtual' => [], 'errors' => [], 'repository' => 'Addresses' } ] ], 'virtual' => [], 'errors' => [], 'repository' => 'Users' } ], 'dirty' => [ 'user' => true, 'genres' => true ], 'original' => [ 'genres' => [ (int) 0 => object(App\Model\Entity\Genre) { 'new' => false, 'accessible' => [ 'description_pt' => true, 'description_en' => true, 'description_es' => true, 'buyers' => true, 'records' => true ], 'properties' => [ 'id' => (int) 3, 'description_pt' => 'Bossa-Nova', 'description_en' => 'Bossa-Nova', 'description_es' => 'Bossa-Nova', '_joinData' => object(App\Model\Entity\BuyersGenre) { 'new' => false, 'accessible' => [ 'genre_id' => true, 'buyer_id' => true, 'genre' => true, 'buyer' => true ], 'properties' => [ 'genre_id' => (int) 3, 'id' => (int) 66, 'buyer_id' => (int) 68 ], 'dirty' => [], 'original' => [], 'virtual' => [], 'errors' => [], 'repository' => 'BuyersGenres' } ], 'dirty' => [], 'original' => [], 'virtual' => [], 'errors' => [], 'repository' => 'Genres' } ] ], 'virtual' => [], 'errors' => [], 'repository' => 'Buyers' }
Sign up for our Newsletter for updates.
http://cakephp.org/newsletter/signup
We will soon be closing this Google Group. But don't worry, we have something better coming. Stay tuned for an updated from the CakePHP Team soon.
Like Us on FaceBook https://www.facebook.com/CakePHP
Follow 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 cake-php+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment