...

View Full Version : cakephp accessing models



hmvrulz
03-22-2009, 10:33 AM
How to access other models tables in a particular model while using CRUD methods.


Have two tables

Restaurant
Phone

Phone has a foreign key restaurant_id.
How can i add a phone number when adding a new entry to restaurant from the same page ?

i have tried

var $uses = Array('Restaurant','Phone');

masterofollies
03-22-2009, 04:58 PM
You can't use a textbox? with a varchar10?

hmvrulz
03-23-2009, 05:52 AM
You can't use a textbox? with a varchar10?

i am confused. what are u pointing out ?

oesxyl
03-23-2009, 06:05 AM
How to access other models tables in a particular model while using CRUD methods.


Have two tables

Restaurant
Phone

Phone has a foreign key restaurant_id.
How can i add a phone number when adding a new entry to restaurant from the same page ?

i have tried

var $uses = Array('Restaurant','Phone');
insert a row in Restaurant table first and then using mysql_insert_id as value for restaurant_id insert one row or more in Phone table.
I assume that you have a column, primary key and is auto_increment in Restaurant( restaurant_id from Phone).

http://www.php.net/manual/en/function.mysql-insert-id.php

best regards

sir pannels
05-05-2009, 12:13 AM
hi hmvrulz,

the correct CakePHP of doing this is to use the $this->save() method... you will have to use it twice, once for your resturant new row and once for your phone table..



if($this->Resturant->save($this->data)){
$this->Phone->create();
$this->data['Phone'] = $this->data['Resturant'];
$this->Phone->save($this->data);
}


all the best,



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum