Re: 3.0 - How to add a form data using its primarykey to its associated tables
i can't understand. could you please give me an example if you have? Note: *I want to create a new record at Costs FixedCosts against Units ID but don't want to add a new record at UnitsTable* Thanks in advance Bayezid On Sun, Dec 14, 2014 at 3:36 AM, José Lorenzo jose@gmail.com wrote: You either need to load the Costs and FixedCosts associations in the get() call or mark the entity with isNew(false) if you want them updated instead of created. On Saturday, December 13, 2014 3:39:34 PM UTC+1, Bayezid Alam wrote: Thanks a lot, but it not saving to Costs FixedCosts Table somehow, i tried, even if its not executing after $this-request-is public function add_cost($uId){ if (!$uId){ throw new NotFoundException(__('Unit id Not found, try with a valid ID')); } $unitId = $this-Units-get($uId); *$unit = $this-Units-patchEntity($unitId, $this-request-data(),['associated' = ['Costs', 'FixedCosts']]);* * debug($unit); // This debug result is below* if ($this-request-is('post')){*// tried adding patch put too, found same result* $unit-cost-unit_id = $unit-fixed_cost-unit_id = $unitId-id; *//debug($unit); // This debug is not executing* if ($this-Units-save($unit)){ $this-Flash-success(__('The unit cost has been added')); return $this-redirect(['controller' = 'houses', 'action' = 'view', $unit-house_id]); } } $this-set(compact('unit')); } *debug($unit):* */src/Controller/UnitsController.php* (line *53*) object(Cake\ORM\Entity) { 'new' = false, 'accessible' = [ '*' = true ], 'properties' = [ 'id' = (int) 1, 'name' = '1st Floor - Front', 'house_id' = (int) 2, 'billing_date' = object(Cake\I18n\Time) { 'time' = '2014-12-05T00:00:00+', 'timezone' = 'UTC', 'fixedNowTime' = false }, 'created' = object(Cake\I18n\Time) { 'time' = '2014-12-10T14:59:25+', 'timezone' = 'UTC', 'fixedNowTime' = false }, 'modified' = object(Cake\I18n\Time) { 'time' = '2014-12-10T14:59:25+', 'timezone' = 'UTC', 'fixedNowTime' = false }, 'cost' = object(Cake\ORM\Entity) { 'new' = true, 'accessible' = [ '*' = true ], 'properties' = [ 'rental_cost' = (float) 2000 ], 'dirty' = [ 'rental_cost' = true ], 'original' = [], 'virtual' = [], 'errors' = [], 'repository' = 'Costs' }, 'fixed_cost' = object(Cake\ORM\Entity) { 'new' = true, 'accessible' = [ '*' = true ], 'properties' = [ 'gas_bill' = (int) 500, 'water_bill' = (int) 300, 'service_bill' = (int) 300 ], 'dirty' = [ 'gas_bill' = true, 'water_bill' = true, 'service_bill' = true ], 'original' = [], 'virtual' = [], 'errors' = [], 'repository' = 'FixedCosts' } ], 'dirty' = [ 'cost' = true, 'fixed_cost' = true ], 'original' = [], 'virtual' = [], 'errors' = [], 'repository' = 'Units' } Thanks Bayezid On Sat, Dec 13, 2014 at 3:45 PM, José Lorenzo jose@gmail.com wrote: Ah ok. I understand the question now. Instead of using newEntity() you should use patchEntity() $unitId = $this-Units-get($uId); $unit = $this-Units-patchEntity($unitId, $this-request-data(),['associated' = ['Costs', 'FixedCosts']]); On Thursday, December 11, 2014 5:08:27 PM UTC+1, Bayezid Alam wrote: Hi, i have Tables : Units hasOne costs hasOne fixed_costs i want to add data to costs and fixed_costs table using units's primary key from units Controller. i did below things but its creating a new record at unit Table instead of using primaryKey Form at unitsController: ?php echo $this-Form-create($unit); echo
Re: 3.0 - How to add a form data using its primarykey to its associated tables
Ah ok. I understand the question now. Instead of using newEntity() you should use patchEntity() $unitId = $this-Units-get($uId); $unit = $this-Units-patchEntity($unitId, $this-request-data(),['associated' = ['Costs', 'FixedCosts']]); On Thursday, December 11, 2014 5:08:27 PM UTC+1, Bayezid Alam wrote: Hi, i have Tables : Units hasOne costs hasOne fixed_costs i want to add data to costs and fixed_costs table using units's primary key from units Controller. i did below things but its creating a new record at unit Table instead of using primaryKey Form at unitsController: ?php echo $this-Form-create($unit); echo $this-Form-input('cost.rental_cost'); echo $this-Form-input('fixed_cost.gas_bill'); echo $this-Form-input('fixed_cost.water_bill'); echo $this-Form-input('fixed_cost.service_bill'); echo $this-Form-button('Save Costs'); echo $this-Form-end(); ? Function at UnitsController: public function add_cost($uId){ if (!$uId){ throw new NotFoundException(__('Unit id Not found, try with a valid ID')); } $unitId = $this-Units-get($uId); $unit = $this-Units-newEntity($this-request-data(),['associated' = ['Costs', 'FixedCosts']]); if ($this-request-is('post')){ $unit-cost-unit_id = $unit-fixed_cost-unit_id = $unitId-id; if ($this-Units-save($unit)){ $this-Flash-success(__('The unit cost has been added')); return $this-redirect(['controller' = 'houses', 'action' = 'view', $unit-house_id]); } } $this-set(compact('unit')); } Please suggest me on this regard. Thanks Bayezid -- 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 cake-php+unsubscr...@googlegroups.com. To post to this group, send email to cake-php@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php. For more options, visit https://groups.google.com/d/optout.
Re: 3.0 - How to add a form data using its primarykey to its associated tables
Thanks a lot, but it not saving to Costs FixedCosts Table somehow, i tried, even if its not executing after $this-request-is public function add_cost($uId){ if (!$uId){ throw new NotFoundException(__('Unit id Not found, try with a valid ID')); } $unitId = $this-Units-get($uId); *$unit = $this-Units-patchEntity($unitId, $this-request-data(),['associated' = ['Costs', 'FixedCosts']]);* * debug($unit); // This debug result is below* if ($this-request-is('post')){*// tried adding patch put too, found same result* $unit-cost-unit_id = $unit-fixed_cost-unit_id = $unitId-id; *//debug($unit); // This debug is not executing* if ($this-Units-save($unit)){ $this-Flash-success(__('The unit cost has been added')); return $this-redirect(['controller' = 'houses', 'action' = 'view', $unit-house_id]); } } $this-set(compact('unit')); } *debug($unit):* */src/Controller/UnitsController.php* (line *53*) object(Cake\ORM\Entity) { 'new' = false, 'accessible' = [ '*' = true ], 'properties' = [ 'id' = (int) 1, 'name' = '1st Floor - Front', 'house_id' = (int) 2, 'billing_date' = object(Cake\I18n\Time) { 'time' = '2014-12-05T00:00:00+', 'timezone' = 'UTC', 'fixedNowTime' = false }, 'created' = object(Cake\I18n\Time) { 'time' = '2014-12-10T14:59:25+', 'timezone' = 'UTC', 'fixedNowTime' = false }, 'modified' = object(Cake\I18n\Time) { 'time' = '2014-12-10T14:59:25+', 'timezone' = 'UTC', 'fixedNowTime' = false }, 'cost' = object(Cake\ORM\Entity) { 'new' = true, 'accessible' = [ '*' = true ], 'properties' = [ 'rental_cost' = (float) 2000 ], 'dirty' = [ 'rental_cost' = true ], 'original' = [], 'virtual' = [], 'errors' = [], 'repository' = 'Costs' }, 'fixed_cost' = object(Cake\ORM\Entity) { 'new' = true, 'accessible' = [ '*' = true ], 'properties' = [ 'gas_bill' = (int) 500, 'water_bill' = (int) 300, 'service_bill' = (int) 300 ], 'dirty' = [ 'gas_bill' = true, 'water_bill' = true, 'service_bill' = true ], 'original' = [], 'virtual' = [], 'errors' = [], 'repository' = 'FixedCosts' } ], 'dirty' = [ 'cost' = true, 'fixed_cost' = true ], 'original' = [], 'virtual' = [], 'errors' = [], 'repository' = 'Units' } Thanks Bayezid On Sat, Dec 13, 2014 at 3:45 PM, José Lorenzo jose@gmail.com wrote: Ah ok. I understand the question now. Instead of using newEntity() you should use patchEntity() $unitId = $this-Units-get($uId); $unit = $this-Units-patchEntity($unitId, $this-request-data(),['associated' = ['Costs', 'FixedCosts']]); On Thursday, December 11, 2014 5:08:27 PM UTC+1, Bayezid Alam wrote: Hi, i have Tables : Units hasOne costs hasOne fixed_costs i want to add data to costs and fixed_costs table using units's primary key from units Controller. i did below things but its creating a new record at unit Table instead of using primaryKey Form at unitsController: ?php echo $this-Form-create($unit); echo $this-Form-input('cost.rental_cost'); echo $this-Form-input('fixed_cost.gas_bill'); echo $this-Form-input('fixed_cost.water_bill'); echo $this-Form-input('fixed_cost.service_bill'); echo $this-Form-button('Save Costs'); echo $this-Form-end(); ? Function at UnitsController: public function add_cost($uId){ if (!$uId){ throw new NotFoundException(__('Unit id Not found, try with a valid ID')); }
Re: 3.0 - How to add a form data using its primarykey to its associated tables
You either need to load the Costs and FixedCosts associations in the get() call or mark the entity with isNew(false) if you want them updated instead of created. On Saturday, December 13, 2014 3:39:34 PM UTC+1, Bayezid Alam wrote: Thanks a lot, but it not saving to Costs FixedCosts Table somehow, i tried, even if its not executing after $this-request-is public function add_cost($uId){ if (!$uId){ throw new NotFoundException(__('Unit id Not found, try with a valid ID')); } $unitId = $this-Units-get($uId); *$unit = $this-Units-patchEntity($unitId, $this-request-data(),['associated' = ['Costs', 'FixedCosts']]);* * debug($unit); // This debug result is below* if ($this-request-is('post')){*// tried adding patch put too, found same result* $unit-cost-unit_id = $unit-fixed_cost-unit_id = $unitId-id; *//debug($unit); // This debug is not executing* if ($this-Units-save($unit)){ $this-Flash-success(__('The unit cost has been added')); return $this-redirect(['controller' = 'houses', 'action' = 'view', $unit-house_id]); } } $this-set(compact('unit')); } *debug($unit):* */src/Controller/UnitsController.php* (line *53*) object(Cake\ORM\Entity) { 'new' = false, 'accessible' = [ '*' = true ], 'properties' = [ 'id' = (int) 1, 'name' = '1st Floor - Front', 'house_id' = (int) 2, 'billing_date' = object(Cake\I18n\Time) { 'time' = '2014-12-05T00:00:00+', 'timezone' = 'UTC', 'fixedNowTime' = false }, 'created' = object(Cake\I18n\Time) { 'time' = '2014-12-10T14:59:25+', 'timezone' = 'UTC', 'fixedNowTime' = false }, 'modified' = object(Cake\I18n\Time) { 'time' = '2014-12-10T14:59:25+', 'timezone' = 'UTC', 'fixedNowTime' = false }, 'cost' = object(Cake\ORM\Entity) { 'new' = true, 'accessible' = [ '*' = true ], 'properties' = [ 'rental_cost' = (float) 2000 ], 'dirty' = [ 'rental_cost' = true ], 'original' = [], 'virtual' = [], 'errors' = [], 'repository' = 'Costs' }, 'fixed_cost' = object(Cake\ORM\Entity) { 'new' = true, 'accessible' = [ '*' = true ], 'properties' = [ 'gas_bill' = (int) 500, 'water_bill' = (int) 300, 'service_bill' = (int) 300 ], 'dirty' = [ 'gas_bill' = true, 'water_bill' = true, 'service_bill' = true ], 'original' = [], 'virtual' = [], 'errors' = [], 'repository' = 'FixedCosts' } ], 'dirty' = [ 'cost' = true, 'fixed_cost' = true ], 'original' = [], 'virtual' = [], 'errors' = [], 'repository' = 'Units' } Thanks Bayezid On Sat, Dec 13, 2014 at 3:45 PM, José Lorenzo jose@gmail.com wrote: Ah ok. I understand the question now. Instead of using newEntity() you should use patchEntity() $unitId = $this-Units-get($uId); $unit = $this-Units-patchEntity($unitId, $this-request-data(),['associated' = ['Costs', 'FixedCosts']]); On Thursday, December 11, 2014 5:08:27 PM UTC+1, Bayezid Alam wrote: Hi, i have Tables : Units hasOne costs hasOne fixed_costs i want to add data to costs and fixed_costs table using units's primary key from units Controller. i did below things but its creating a new record at unit Table instead of using primaryKey Form at unitsController: ?php echo $this-Form-create($unit); echo $this-Form-input('cost.rental_cost'); echo $this-Form-input('fixed_cost.gas_bill'); echo $this-Form-input('fixed_cost.water_bill'); echo
Re: 3.0 - How to add a form data using its primarykey to its associated tables
Can you show how your UnitsTable look like? Can you also show a debug($unit) before you all save() ? On Thursday, December 11, 2014 5:08:27 PM UTC+1, Bayezid Alam wrote: Hi, i have Tables : Units hasOne costs hasOne fixed_costs i want to add data to costs and fixed_costs table using units's primary key from units Controller. i did below things but its creating a new record at unit Table instead of using primaryKey Form at unitsController: ?php echo $this-Form-create($unit); echo $this-Form-input('cost.rental_cost'); echo $this-Form-input('fixed_cost.gas_bill'); echo $this-Form-input('fixed_cost.water_bill'); echo $this-Form-input('fixed_cost.service_bill'); echo $this-Form-button('Save Costs'); echo $this-Form-end(); ? Function at UnitsController: public function add_cost($uId){ if (!$uId){ throw new NotFoundException(__('Unit id Not found, try with a valid ID')); } $unitId = $this-Units-get($uId); $unit = $this-Units-newEntity($this-request-data(),['associated' = ['Costs', 'FixedCosts']]); if ($this-request-is('post')){ $unit-cost-unit_id = $unit-fixed_cost-unit_id = $unitId-id; if ($this-Units-save($unit)){ $this-Flash-success(__('The unit cost has been added')); return $this-redirect(['controller' = 'houses', 'action' = 'view', $unit-house_id]); } } $this-set(compact('unit')); } Please suggest me on this regard. Thanks Bayezid -- 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 cake-php+unsubscr...@googlegroups.com. To post to this group, send email to cake-php@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php. For more options, visit https://groups.google.com/d/optout.
Re: 3.0 - How to add a form data using its primarykey to its associated tables
Hello Lerenzo, Please find the UnitsTable and debug($unit) as follows: *UnitsTable:* ?php namespace App\Model\Table; use Cake\ORM\Table; class UnitsTable extends Table { public function initialize(array $config) { $this-table('units'); $this-addBehavior('Timestamp'); $this-primaryKey('id'); // association goes here $this-belongsTo('Houses',[ 'foreignKey' = 'house_id' ]); $this-hasOne('Costs',[ 'foreignKey' = 'unit_id' ]); $this-hasOne('FixedCosts',[ 'foreignKey' = 'unit_id' ]); } } ? *debug($unit):* */src/Controller/UnitsController.php* (line *53*) object(Cake\ORM\Entity) { 'new' = true, 'accessible' = [ '*' = true ], 'properties' = [ 'cost' = object(Cake\ORM\Entity) { 'new' = true, 'accessible' = [ '*' = true ], 'properties' = [ 'rental_cost' = (float) 14500 ], 'dirty' = [ 'rental_cost' = true ], 'original' = [], 'virtual' = [], 'errors' = [], 'repository' = 'Costs' }, 'fixed_cost' = object(Cake\ORM\Entity) { 'new' = true, 'accessible' = [ '*' = true ], 'properties' = [ 'gas_bill' = (int) 450, 'water_bill' = (int) 600, 'service_bill' = (int) 500 ], 'dirty' = [ 'gas_bill' = true, 'water_bill' = true, 'service_bill' = true ], 'original' = [], 'virtual' = [], 'errors' = [], 'repository' = 'FixedCosts' } ], 'dirty' = [ 'cost' = true, 'fixed_cost' = true ], 'original' = [], 'virtual' = [], 'errors' = [], 'repository' = 'Units' } Thanks Bayezid On Fri, Dec 12, 2014 at 3:02 PM, José Lorenzo jose@gmail.com wrote: Can you show how your UnitsTable look like? Can you also show a debug($unit) before you all save() ? On Thursday, December 11, 2014 5:08:27 PM UTC+1, Bayezid Alam wrote: Hi, i have Tables : Units hasOne costs hasOne fixed_costs i want to add data to costs and fixed_costs table using units's primary key from units Controller. i did below things but its creating a new record at unit Table instead of using primaryKey Form at unitsController: ?php echo $this-Form-create($unit); echo $this-Form-input('cost.rental_cost'); echo $this-Form-input('fixed_cost.gas_bill'); echo $this-Form-input('fixed_cost.water_bill'); echo $this-Form-input('fixed_cost.service_bill'); echo $this-Form-button('Save Costs'); echo $this-Form-end(); ? Function at UnitsController: public function add_cost($uId){ if (!$uId){ throw new NotFoundException(__('Unit id Not found, try with a valid ID')); } $unitId = $this-Units-get($uId); $unit = $this-Units-newEntity($this-request-data(),['associated' = ['Costs', 'FixedCosts']]); if ($this-request-is('post')){ $unit-cost-unit_id = $unit-fixed_cost-unit_id = $unitId-id; if ($this-Units-save($unit)){ $this-Flash-success(__('The unit cost has been added')); return $this-redirect(['controller' = 'houses', 'action' = 'view', $unit-house_id]); } } $this-set(compact('unit')); } Please suggest me on this regard. Thanks Bayezid -- 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 cake-php+unsubscr...@googlegroups.com. To post to this group, send email to cake-php@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php. For more options, visit https://groups.google.com/d/optout. -- 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
Re: 3.0 - How to add a form data using its primarykey to its associated tables
Hello Lorenzo, please Ignore Previous debug($unit) find the latest debug($unit) before all save() */src/Controller/UnitsController.php* (line *59*) object(Cake\ORM\Entity) { 'new' = true, 'accessible' = [ '*' = true ], 'properties' = [ 'cost' = object(Cake\ORM\Entity) { 'new' = true, 'accessible' = [ '*' = true ], 'properties' = [ 'rental_cost' = (float) 14500, 'unit_id' = (int) 1 ], 'dirty' = [ 'rental_cost' = true, 'unit_id' = true ], 'original' = [], 'virtual' = [], 'errors' = [], 'repository' = 'Costs' }, 'fixed_cost' = object(Cake\ORM\Entity) { 'new' = true, 'accessible' = [ '*' = true ], 'properties' = [ 'gas_bill' = (int) 450, 'water_bill' = (int) 600, 'service_bill' = (int) 500, 'unit_id' = (int) 1 ], 'dirty' = [ 'gas_bill' = true, 'water_bill' = true, 'service_bill' = true, 'unit_id' = true ], 'original' = [], 'virtual' = [], 'errors' = [], 'repository' = 'FixedCosts' } ], 'dirty' = [ 'cost' = true, 'fixed_cost' = true ], 'original' = [], 'virtual' = [], 'errors' = [], 'repository' = 'Units' } Note: A new record is being saved in UnitsTable but i don't want to Thanks Bayezid On Fri, Dec 12, 2014 at 10:35 PM, Md Bayezid Alam bayezid...@gmail.com wrote: Hello Lerenzo, Please find the UnitsTable and debug($unit) as follows: *UnitsTable:* ?php namespace App\Model\Table; use Cake\ORM\Table; class UnitsTable extends Table { public function initialize(array $config) { $this-table('units'); $this-addBehavior('Timestamp'); $this-primaryKey('id'); // association goes here $this-belongsTo('Houses',[ 'foreignKey' = 'house_id' ]); $this-hasOne('Costs',[ 'foreignKey' = 'unit_id' ]); $this-hasOne('FixedCosts',[ 'foreignKey' = 'unit_id' ]); } } ? *debug($unit):* */src/Controller/UnitsController.php* (line *53*) object(Cake\ORM\Entity) { 'new' = true, 'accessible' = [ '*' = true ], 'properties' = [ 'cost' = object(Cake\ORM\Entity) { 'new' = true, 'accessible' = [ '*' = true ], 'properties' = [ 'rental_cost' = (float) 14500 ], 'dirty' = [ 'rental_cost' = true ], 'original' = [], 'virtual' = [], 'errors' = [], 'repository' = 'Costs' }, 'fixed_cost' = object(Cake\ORM\Entity) { 'new' = true, 'accessible' = [ '*' = true ], 'properties' = [ 'gas_bill' = (int) 450, 'water_bill' = (int) 600, 'service_bill' = (int) 500 ], 'dirty' = [ 'gas_bill' = true, 'water_bill' = true, 'service_bill' = true ], 'original' = [], 'virtual' = [], 'errors' = [], 'repository' = 'FixedCosts' } ], 'dirty' = [ 'cost' = true, 'fixed_cost' = true ], 'original' = [], 'virtual' = [], 'errors' = [], 'repository' = 'Units' } Thanks Bayezid On Fri, Dec 12, 2014 at
Re: 3.0 - How to add a form data using its primarykey to its associated tables
HI May i get a suggestion on this? I did google but found nothing. Thanks Bayezid On Thu, Dec 11, 2014 at 10:07 PM, Md Bayezid Alam bayezid...@gmail.com wrote: Hi, i have Tables : Units hasOne costs hasOne fixed_costs i want to add data to costs and fixed_costs table using units's primary key from units Controller. i did below things but its creating a new record at unit Table instead of using primaryKey Form at unitsController Template: ?php echo $this-Form-create($unit); echo $this-Form-input('cost.rental_cost'); echo $this-Form-input('fixed_cost.gas_bill'); echo $this-Form-input('fixed_cost.water_bill'); echo $this-Form-input('fixed_cost.service_bill'); echo $this-Form-button('Save Costs'); echo $this-Form-end(); ? Function at UnitsController: public function add_cost($uId){ if (!$uId){ throw new NotFoundException(__('Unit id Not found, try with a valid ID')); } $unitId = $this-Units-get($uId); $unit = $this-Units-newEntity($this-request-data(),['associated' = ['Costs', 'FixedCosts']]); if ($this-request-is('post')){ $unit-cost-unit_id = $unit-fixed_cost-unit_id = $unitId-id; if ($this-Units-save($unit)){ $this-Flash-success(__('The unit cost has been added')); return $this-redirect(['controller' = 'houses', 'action' = 'view', $unit-house_id]); } } $this-set(compact('unit')); } Please suggest me on this regard. Thanks Bayezid -- 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 cake-php+unsubscr...@googlegroups.com. To post to this group, send email to cake-php@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php. For more options, visit https://groups.google.com/d/optout.