Saturday, December 13, 2014

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+0000',  			'timezone' => 'UTC',  			'fixedNowTime' => false  		  		},  		'created' => object(Cake\I18n\Time) {    			'time' => '2014-12-10T14:59:25+0000',  			'timezone' => 'UTC',  			'fixedNowTime' => false  		  		},  		'modified' => object(Cake\I18n\Time) {    			'time' => '2014-12-10T14:59:25+0000',  			'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.zap@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'));
        }
        
        $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+unsubscribe@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 it, send an email to cake-php+unsubscribe@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.

No comments: