Cake Php在另一个控制器中找到一些东西

时间:2014-04-18 17:51:09

标签: php cakephp

我之前试图简化这个问题。这是真正的代码

我没有找到'找到'的问题。我发现后抓取数据时遇到问题。我用它来验证。如果我没有使用它来验证我可以在页面上显示print_r并获得我的变量。但我在这个中间地带工作,我不知道如何找到我的变种。我安装了调试器。

我没有使用用户身份验证,因为这是一个快速的项目,我还不是它的主人,安全对于这个项目来说并不是什么大问题。

我的桌子

用户 [ID] [名称] [主治] [极限]

客 [ID] [USER_ID] [名称]

public function add($id = null) {



    if ($this->request->is('post')) {
        $this->Guest->create();

        $user_id = $this->request->data['Guest']['user_id'];
        //i have a hidden input in the from that is why this works, i know this isn't the right way tto do this.

        $guest_count = $this->Guest->find('count', array(
                                                        'conditions' => 
                                                            array('user_id' => $user_id )
                                                        )
                                                    );

        $userData = $this->Guest->User->find('all', array('id' => $user_id ));
        //i got this this works


        $guest_limit = $userData['limit'];//this doesn't work


        if($guest_count >= $guest_limit)
        {

            $this->Session->setFlash(__('You can\'t add any more guest '));
            $this->set('dataView', $this->request->data );
            return $this->redirect(array('controller' => 'Users', 'action' => 'view' , $id));

        } else {

            if ($this->Guest->save($this->request->data)) {
                $this->Session->setFlash(__('Guest added'));
                $this->set('dataView', $this->request->data );
                return $this->redirect(array('controller' => 'Users', 'action' => 'view' , $id));
            } else {
                $this->Session->setFlash(__('Invalid name entered'));
                return $this->redirect(array('controller' => 'Users', 'action' => 'view' , $id));

            }
        }
    }
    /**/

2 个答案:

答案 0 :(得分:0)

有很多方法可以做到这一点 - 这是一个简单的方法 -

Guest.php

class Guest extends AppModel{
   public $hasMany = array('Item');

}

On GuestsController.php

public function view($id = null) {
$user_id = $id;
$guest_with_item = $this->Guest->Item->find('all', array(
                                                'conditions' => array(
                                                    'user_id' => $user_id )
                                                ),
                                               'recursive' => 1
                                            ); 
}

更新问题后

$userData = $this->Guest->User->find('first', array(
                                                'conditions' => array(
                                                    'User.id' => $user_id
                                                )
                                            )
                                        );
 $guest_limit = $userData['User']['limit'];

答案 1 :(得分:0)

<强> AppModel.php

class AppModel extends Model {
    public $recursive = -1;
    public $actsAs = array(
        'Containable'
    );
}

<强> Item.php

class Item extends AppModel {
    public $belongsTo = array('Guest');
}

<强> Guest.php

class Guest extends AppModel {
    public $hasMany = array('Item');
}

访客控制器

//or find first. it depends on how many rows with the same user_id You can have.
$this->Guest->find('all', array(
    'conditions' => array(
        'Guest.user_id' => $user_id
    ),
    'contain' => array(
        'Item'
    )
));

是的,您应该多阅读Book几次