I am using cakePHP 2.5, and i set up two models: Vehicle Model (pk is vei_id) Address Model (pk is adr_id and fk is vei_id).
I try to map the association using hasOne from Vehicle to Address, i do add records into Vehicle, but could not insert records into Address table :
class Vehicle extends AppModel
{
public $name = 'Address';
public $primaryKey = 'adr_id';
public $useTable = 'addresses';
public $hasOne = array('Vehicle'=>array(
'className' => 'Vehicle',
'foreignKey' => 'vei_id',
)
);
}
Also add a BelongsTo into the Address model :
class Address extends AppModel
{
public $belongsTo = array(
'Address' => array(
'className' => 'Address',
'foreignKey' => 'adr_id',
)
);
The SQL error is :
[message] => ORA-01400: not posssible insert NULL into ("ADDRESS"."VEI_ID")
At the controller i do use saveAll. The Vehicle record is saved but the Address record is not saved :
if ($this->validates()) {
$saveRequest['Vehicle'] = $this->request->data['Vehicle'];
$saveRequest['Address'] = $this->request->data['Address'];
if ($this->Vehicle->saveAll($saveRequest, array('deep' => true))) {
$this->Session->setFlash($this->messageSuccess, 'flash_success');
$this->redirect = array( 'action' => 'ver',$this->Vehicle->id );
}
How can i properly set up the association between the two models and save the associated data ?
It seems problems in your table relation. Your relation should be like this way.
Vehicle Table
class Vehicle extends AppModel
{
public $name = 'Vehicle';
public $primaryKey = 'id';
public $useTable = 'vehicles';
public $hasOne = array(
'Address' => array(
'className' => 'Address',
'foreignKey' => 'vei_id',
)
);
}
Address Table:
class Address extends AppModel
{
public $name = 'Address';
public $belongsTo = array(
'Vehicle' => array(
'className' => 'Vehicle',
'foreignKey' => 'vei_id',
)
);
}
You might need to change ‘$useTable’ nad ‘foreignKey’ value according to your setup. (database table name, foreign key name etc).
Ensure these value correctly and then try again (NB: no need to change if you have table called vehicles and vei_id column in your address table). Hope it will work!
It works, i do use
class Vehicle extends AppModel
{
public $name = 'Vehicle';
public $primaryKey = 'vei_id';
public $useTable = 'vehicles';
public $hasOne = array(
'Address' => array(
'className' => 'Address',
'conditions' => 'Vehicle.vei_id = Address.vei_id',
'foreignkey' => false
)
);
}
And
class Address extends AppModel
{
public $name = 'Address';
public $belongsTo = array(
'Vehicle' => array(
'className' => 'Vehicle',
'foreignKey' => 'vei_id',
)
);
}
It records on both tables that´s fine! now my problem is that a join try to guess the join condition with a column that does not exists, even if i declare the join into the hasOne property.
$data = $this->Vehicle->find(‘first’, array(
‘conditions’ => array(
‘Vehicle.vei_id’ => $vehicleId
),
‘contain’ => array(
‘Address’
),
));
It try to join using :vehicle.vehicle_id wich does not exists
Try this one
$data = $this->Vehicle->find('first', array(
'conditions' => array(
'Vehicle.vei_id' => $vehicleId
),
'contain' => array(
'Address' => array(
'conditions' => array('Address.vei_id' => $vehicleId),
)
)
));
Thank you @sohelrana820
i try using the condition in every model and at the controller query
//Vehicle
public $hasOne = array(
'Address' => array(
'className' => 'Address',
'conditions' => array('Vehicle.vei_id = Address.vei_id'),
'foreignkey' => false
)
//Address
public $belongsTo = array(
'Vehicle' => array(
'className' => 'Vehicle',
'conditions'=>array('Vehicle.vei_id=Address.vei_id'),
'foreignKey' => 'vei_id'
),
);
//At vehiclecontroller
$data = $this->Vehicle->find('first', array(
'conditions' => array(
'Vehicle.vei_id' => $vehicleId
),
'contain' => array(
'Address' => array(
'conditions'=> array('Address.vei_id'=>'Vehicle.vei_id',
'Vehicle.vei_id' => $vehicleId
),
)
),
));
But still have a condition using a non existing column , the vehicle_id column !
Is there a way to disable this condition ?
It generates:
Address.vehicle_id = Vehicle.vei_id AND Address.vei_id = ‘Vehicle.vei_id’ AND Vehicle.vei_id = 123
Can i nullify or avoid this condition : Address.vehicle_id = Vehicle.vei_id ?
Seems the CakePHP Framework try to automatically sugest a JOIN condition.
But there is no vehicle_id collumn .
Even writing into the two models the right conditions that uses vei_id column (the only column that exists to join these two tables) .
I am using CAKEPHP 2.5 version .
@angelorigo Can you share your database schema of these two tables (vehicle and address). I guess you didn’t follow the cakehpp convention properly. See this article https://book.cakephp.org/2.0/en/models/associations-linking-models-together.html.
Hi @sohelrana820
In this case i could not follow the conventions.
It is a legacy app and CakePHP was implemented after the app already exists.
I thought that using ‘foreignKey’ => ‘vei_id’ i could handle the join conditions
I just need to avoid a jon generated into a wrong key : Vehicle.vehicle_id = Address.vei_id
I need it to be : Vehicle.vei_id = Address.vei_id