CakePhp相关组合框不充电

时间:2013-11-20 16:40:23

标签: php cakephp combobox models

我一直在使用CakePhp中的一个项目使用3个表(Benefiaciries,Programs and Beneficiaries_Programs),第3个表只是为了建立正确的关系,一个受益人可以有一个或多个程序。我的问题是我需要在一个步骤中将受益人和他们的程序一起保存,但是当我尝试加载程序的组合时,这显示为空,我已经尽力但仍然无法解决它。

模特(关系)

//Beneficiary
    public $hasMany = array(
    'Personsprogram' => array(
        'className' => 'Personsprogram',
        'foreignKey' => 'beneficiary_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    ),'Program'
);

//Program
public $hasMany = array(
    'Personsprogram' => array(
        'className' => 'Personsprogram',
        'foreignKey' => 'program_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);

//Personsprogram
    public $belongs = array(
    'Beneficiary' => array(
        'className' => 'Beneficiary',
        'foreignKey' => 'beneficiary_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Program' => array(
        'className' => 'Program',
        'foreignKey' => 'program_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

受益人观点(添加)

<?php
//This is the combo what was supposed to be charging the programs so I could pick one.
    echo $this->Form->input('Personsprogram.program_id', array('label'=>'Programs'));
    echo $this->Form->input('date');
    echo $this->Form->input('locality_id');
    echo $this->Form->input('f_surname');
    echo $this->Form->input('s_surname');
    echo $this->Form->input('first_name');
    echo $this->Form->input('curp');
    echo $this->Form->input('sex');
?>

控制器

<?php
App::uses('AppController', 'Controller');
/**
 * Beneficiaries Controller
 *
 * @property Beneficiary $Beneficiary
 * @property PaginatorComponent $Paginator
 */
App::uses('Personsprogram', 'Model');
App::uses('Program', 'Model');
class BeneficiariesController extends AppController {
//public $uses = array('Locality','Program','Municipality','Personsprogram');

/**
 * Components
 *
 * @var array
 */
    public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
    public function index() {
        $this->Beneficiary->recursive = 0;
        $this->set('beneficiaries', $this->Paginator->paginate());
    }

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function view($id = null) {
        if (!$this->Beneficiary->exists($id)) {
            throw new NotFoundException(__('Invalid beneficiary'));
        }
        $options = array('conditions' => array('Beneficiary.' .     $this->Beneficiary->primaryKey => $id));
        $this->set('beneficiary', $this->Beneficiary->find('first', $options));
    }

/**
 * add method
 *
 * @return void
 */
    public function add() {
        if ($this->request->is('post')) {
            $this->Beneficiary->create();
            if ($this->Beneficiary->save($this->request->data)) {
                $this->Session->setFlash(__('The beneficiary has been     saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The beneficiary could not be     saved. Please, try again.'));
            }
        }
        $localities = $this->Beneficiary->Locality->find('list');
        $nutritionists = $this->Beneficiary->Nutritionist->find('list');
        $this->set(compact('localities', 'nutritionists'));
    }

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function edit($id = null) {
        if (!$this->Beneficiary->exists($id)) {
            throw new NotFoundException(__('Invalid beneficiary'));
        }
        if ($this->request->is(array('post', 'put'))) {
            if ($this->Beneficiary->save($this->request->data)) {
                $this->Session->setFlash(__('The beneficiary has been     saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The beneficiary could not be     saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('Beneficiary.' .     $this->Beneficiary->primaryKey => $id));
            $this->request->data = $this->Beneficiary->find('first', $options);
        }
        $localities = $this->Beneficiary->Locality->find('list');
        $nutritionists = $this->Beneficiary->Nutritionist->find('list');
        $this->set(compact('localities', 'nutritionists'));
    }

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function delete($id = null) {
        $this->Beneficiary->id = $id;
        if (!$this->Beneficiary->exists()) {
            throw new NotFoundException(__('Invalid beneficiary'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Beneficiary->delete()) {
            $this->Session->setFlash(__('The beneficiary has been deleted.'));
        } else {
            $this->Session->setFlash(__('The beneficiary could not be deleted.     Please, try again.'));
        }
        return $this->redirect(array('action' => 'index'));
    }}
   ?>

所以组合除了空字段外没有显示任何内容。

我很感激任何建议或帮助,你可以告诉我我做错了什么。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

我经过长时间的研究后想出了如何解决这个问题......

我所要做的就是将其添加到“ADD”控制器

    $dependencies = $this->Beneficiary->Personsprogram->Program->Dependency->find('list');
    $programs = $this->Beneficiary->Personsprogram->Program->find('list', array(
    'fields' => array('Program.id', 'Program.name'),
    'conditions' => array('Program.name LIKE' => '%%'),
    'recursive' => 0));
    $this->set(compact('dependencies','programs'));

所以现在我得到了我的依赖项和程序列表,在我的视图中我只需要使用

调用该字段
$this->Form->input("my_field");