Results 1 to 5 of 5

Thread: How to get id of another model in CakePHP?

  1. #1
    Join Date
    Sep 2010
    Posts
    13

    How to get id of another model in CakePHP?

    I have 2 models. A model and a model Member User. When I created a new member, the controller of a Member, call the function to create the User with requestAction. So the user is created in the User controller and controller to return to the Member, I would like to retrieve the id of the user to add in the member table. I think that I have tried my best to explain you members my problem.

  2. #2
    Join Date
    Oct 2008
    Posts
    101

    Re: How to get id of another model in CakePHP?

    You should look towards the saveAll method in the doc chapter model, rather than using requestAction. Indeed, a look at saveAll (), which is handy. Otherwise, by doing something like $ this-> User-> Member-> save ($ this-> data); I think it will work well. And according to me, it would be better if you have provided some coding because it would be easy to find out the problems that you are getting.

  3. #3
    Join Date
    Sep 2010
    Posts
    13

    Re: How to get id of another model in CakePHP?

    I'm still stuck in the same type of problem. I wish I could save data to a different model of controller in which I am ... In other words, I'm in the Controller User, I save the data from User ($ this-> User-> save ($ this-> data)), I get this newly created id, $ This-> data ['Artist'] ['user_id'] = mysql_insert_id (); and I would just saved this id in my table BIOGRAPHY $ This-> Artist-> save ($ this-> data); but I have this error: Fatal error: Call to a member function save () is a non-object. How could just save this id (with an insert base if you must) in cakephp ...

  4. #4
    Join Date
    Aug 2008
    Posts
    129

    Re: How to get id of another model in CakePHP?

    Firstly I advise you not to not retrieve the id created with newly mysql_insert_id () but by simply calling $ this-> User-> id. Second, whether the relationship between your model is well defined (User hasMany Artist I guess), you can do this:
    Code:
      $ This-> User-> save ($ this-> data);
     $ This-> data = null;
     $ This-> data [Artist] ['user_id'] = $ this-> User-> id;
     $ This-> User-> Artist-> create ($ this-> data);
     $ This-> User-> Artist-> save ();

  5. #5
    Join Date
    Dec 2008
    Posts
    87

    Re: How to get id of another model in CakePHP?

    Hello, you can also do this:
    In your Controller User:
    Code:
    var $ uses = array ('User', 'Artist');
    Thus, you can directly use the two models in the same controller ... You do not have to create a controller for model. And to retrieve the id of a new record, you can do this:
    Code:
    if ($ this-> User-> save ($ this-> data ['User']))
     {
     $ User_id = $ this-> User-> getInsertID ();
     //...
     }
    That is, if it can be useful to you. The method getInsertID () is deprecated for a while, instead using the technique described by Rixwel above, ie $this->User->id; which always contains the id of the last record. Also, yes you can add all the models in $uses , but this is not a good practice because it includes unnecessary overload achieved. It should do so only for models with no relationship between them, otherwise we use the winner chaining of models:
    $this->Model->OtherModel->find()

Similar Threads

  1. CakePHP vs. Ruby on Rails
    By pele1200 in forum Polls & Voting
    Replies: 9
    Last Post: 03-08-2012, 05:26 PM
  2. Replies: 8
    Last Post: 05-12-2011, 09:20 PM
  3. Links between models in CakePHP
    By Dante i in forum Software Development
    Replies: 4
    Last Post: 05-01-2011, 07:43 AM
  4. Need help with a project in CakePHP
    By Ander K in forum Software Development
    Replies: 4
    Last Post: 05-01-2011, 12:09 AM
  5. What are basic Principles of CakePHP?
    By Xanty in forum Software Development
    Replies: 6
    Last Post: 04-01-2011, 11:25 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,229,376.93602 seconds with 17 queries