thanks see I have this problem I want to check if my id (of my user), exist into a other table if this not exist redirect to my add.
table1
| id |
| username |
| password |
| creation |
| type |
table2
| id |
| id_user |
| cant |
| medaza |
| cachaza |
I don't want to use hasone because is not for this case.
I want compare id==id_user exist a session for this. controller
class IngeniosController extends AppController
{
public function add()
{
}
public function index()
{
$this->loadModel('Ingenio');
$conditions = array(
'conditions' => array(
'UserAzucar.id_user' => $this->Session->read('User.id')
)
);
$result = $this->User->find('first', $conditions);
if (isset($result['User'])){
$this->redirect(array('action' => 'add'));
}
}
public function delete()
{
}
}
model
<?php
/**
*
*/
class Ingenio extends AppModel
{
public $name = 'UserAzucar';
public $useTable = 'usuarios_azucar';
public $primaryKey = 'id_azucarusuario';
}
?>
table2 is user_azucar
or usuarios_azucar
?
And why the model Name is different from the $name
attribute?
You are using strange conventions for your names (why don't you follow cake conventions?)
but if I understand your schema then you have to do this:
$conditions = array(
'conditions' => array(
'id_user' => $this->Session->read('User.id')
)
);
$result = $this->Ingenio->find('first', $conditions);
if (empty($result)){
$this->redirect(array('action' => 'add'));
}
Just replace the controller with the below code
class IngeniosController extends AppController {
public function add()
{
}
public function index(){
$this->loadModel('User');
$result_count = $this->User->find('count', array(
'joins' => array(
array(
'table' => 'user_azucar',
'alias' => 'UserAzucar',
'type' => 'RIGHT',
'conditions' => array(
'UserAzucar.id_user = User.id'
)
)
),
'conditions' => array(
'UserAzucar.id_user' => $this->Session->read('User.id')
)
));
if(!$result_count){
$this->redirect(array('action' => 'add'));
}
}
public function delete()
{
}
}
do this in your appController and top of IngeniosController
public $components = array('Session','RequestHandler');
debug your SESSION to make sure if it is not empty
var_dump($this->Session->read('User.id'));
Your controller:
public function index()
{
$this->loadModel('UserAzucar');
$conditions = array(
'conditions' => array(
'id_user' => $this->Session->read('User.id')
)
);
$this->UserAzucar->recursive = -1;
$result = $this->UserAzucar->find('first', $conditions);
if (empty($result)){
return $this->redirect(array('action' => 'add'));
}
}