Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2007
    Posts
    137
    Thanks
    5
    Thanked 0 Times in 0 Posts

    cakephp accessing models

    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');

  • #2
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    You can't use a textbox? with a varchar10?

  • #3
    Regular Coder
    Join Date
    Jan 2007
    Posts
    137
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by masterofollies View Post
    You can't use a textbox? with a varchar10?
    i am confused. what are u pointing out ?

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by hmvrulz View Post
    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/functio...-insert-id.php

    best regards

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    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..

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

    all the best,


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •