选择中间表的行

时间:2016-02-25 15:13:38

标签: php cakephp

我在第一个cakephp项目中说明。

这是我的活动模型

class Event extends AppModel {

//relacion con user un evento es creado por un usuario
public $belongsTo = array(
        'User' => array('className' => 'User',
                          'foreingKey' => 'user_id'   
                        )
            );


//relacion n:m con usuarios asisten a muchos eventos
public $hasAndBelongsToMany = array(
        'User' => array(
                'className' => 'User',
                'joinTable' => 'events_users', //lineamuyimportante!! 
                'foreingKey' => 'event_id',
                'associationForeingKey' => 'user_id',
                'finderQuery' => 'SELECT users_users.user_id2 FROM users_users join events on users_users.user_id = events.user_id where events.user_id =  {$__cakeID__$}' 
            )
        );

public $hasMany = array(
        'File' => array('className' => 'File',
                          'foreingKey' => 'event_id',
                          'dependent' => true
                        )
            );

这是我的eventcontroller

class EventsController extends AppController {
public $helpers = array('Paginator');

    public $components = array(
            'DebugKit.Toolbar',
            'Session',
            'Auth' => array(
                'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
                'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
                'authError' => 'You must be logged in to view this page.',
                'loginError' => 'Invalid Username or Password entered, please try again.',
                'authenticate' => array(
                'Form' => array(
                    'fields' => array(
                      'username' =>  'email', //Default is 'username' in the userModel
                      'password' => 'password'  //Default is 'password' in the userModel
                    )
                )

            )
    ));

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('login','add'); 
    }

    public function listarTodos() {
        $options = array('conditions'=>array('user_id'=>$this->Auth->user('id')));
        $this->set('eventos', $this->Event->find('all', $options));
    }   

     public function listarProximos() {
        $options = array('conditions'=>array('user_id'=>$this->Auth->user('id'),'fecha >=' => date('Y-m-d') ));
        $this->set('eventos', $this->Event->find('all', $options));
    } 



    public function add(){
        if($this->request->is('post')):
        $this->request->data['Event']['user_id'] = $this->Auth->user('id');
            $this->Event->create();
            if($this->Event->save($this->request->data)):
                $this->Session->setFlash('Event guardado');
                $this->redirect(array('controller' => 'users','action' => 'index'));
            endif;
        endif;
        $users = $this->Event->User->find('list', array('conditions' => array('id' => $this->Auth->user('id') )));
        $this->set(compact('users'));  
    }

我将解释我的项目:
我有用户,活动和联系人。我在用户中有很多关于事件和很多用户到事件。我在用户中有很多用户。 用户创建许多事件和许多用户参加的事件。

我无法创建一个视图,我选择用户作为联系人。

我创建了一个视图,我可以选择所有用户。

0 个答案:

没有答案