Reputation: 647
i have created one function in cakephp appcontrollet.php file. that retrive me the detail on that record from that id the function is mentioned below
function getDetail($id = null) // to check that user is valid or not
{
$data = $this->ModelName->find('first',array('conditions'=>array('ModelName.is_active'=>'Y','ModelName.is_deleted'=>'N','ModelName.primaryKey'=>$id)));
if($data !=array())
{
// return true;
return $data;
} else {
$data = array();
return $data;
//return false;
}
}
Now i want to call this function from any controlller to get detail of the record like i will call this function fron Userscontroller,adminscontroller, marketscontroller etc. and it will return me the related data
my issue is that how should appcontroller know that request is come from which controller and which model to user ?
can anyone help me to solve this
thanks in advance
Upvotes: 0
Views: 1800
Reputation: 1158
Put this function to AppModel and call it from each controller with the related model. The function then looks like:
class AppModel extends Model
{
public function getDetail($id = null) {
$data = $this->find('all', array(
'conditions' => array($this->name.'.id' => $id)
));
// ...
Depending on your call from the model it will automatically use the right model. Such a call may look like this:
class UsersController extends AppController
{
public $uses = array('User', 'Market');
public function index()
{
// Fetch data from markets database
$this->Market->getDetail($id);
// Fetch data from users database
$this->User->getDetail($id);
// ...
In this case, the model Market will be used to fetch the details from the database.
Upvotes: 1