Reputation: 647
hey i am new in cake php. I want to use one model in another model then we can user $uses method. this method is very easy. we can also do the same thing with App::import('Controller', 'Users'); ? if yes then tell me how ?
Upvotes: 4
Views: 626
Reputation: 4866
Yes you can but not with the $uses
property as the Model doesn't have one.
Take a look at this definition of $uses
:
An array containing the class names of models this controller uses.
So what this means is that $uses
is a Controller property, not a Model one.
Now using one model's functionality in another is possible of course. That is what Model Associations are for. For example if you have the following:
Recipe belongsTo User and the opposite association (for e.g.): User hasMany Recipe (A user can have multiple recipes.) from both of these models you would be able to call the other's methods with:
From the User model: $this->Recipe->find('all');
will do a find on the recipes table. Any custom methods defined in the other model can also be called.
If two models do not have a association defined you can do "on the fly" binding. You can read all about these capabilities in the CakePHP Book - 1.3 or 2.x.
And a friendly advice - I see you posting this question with the cakephp1.3 tag, If you are new I would strongly recommend starting to learn CakePHP 2.x because this version:
1. is the newest
2. has the most "object approach"
3. is the one that will be developed even more (not that 1.3 is not being maintained or anything)
Upvotes: 1