【问题标题】:Method find() not working in CakePHP方法 find() 在 CakePHP 中不起作用
【发布时间】:2013-07-04 19:41:04
【问题描述】:

我正在尝试使用 find('list') 方法,但没有成功。

当我尝试列出支付类型时,只有一个数据库表显示正确,另一个正在执行正确的 SQL,但它返回 null(即使使用 sql 获取行)。

如果尝试读取 Tpagamento 的 ONE 字段,它可以工作 如果我将 Tpagamento 的 displayField 更改为“id”,它可以工作,但查询只显示 id。

这是场景:

(Parcelamento 和 Tpagamento 具有相同的数据库字段)

查看方法:

public function view($id = null) {

    $this->Ordemservico->id = $id;
    if (!$this->Ordemservico->exists()) {
        $this->Session->setFlash('This Order does not exist..','flash_error');
        $this->redirect(array('action' => 'index'));
    }

            //Listing all orders // THIS IS WORKING
    $os = $this->Ordemservico->read(null, $id);
    $this->set('os',$os);

    //Listing the types of paying //THIS  IS WORKING
    $this->loadModel('Parcelamento');
    $parcelamentos = $this->Parcelamento->find('list');
    $this->set('parcelamentos',$parcelamentos);


    $this->loadModel('Tpagamento'); // THIS IS NOT WORKING
    $tpagamento = $this->Tpagamento->find('list');
    $this->set('tpagamento ',$tpagamento );

            //Read a specific type of paying, WORK!
    $this->loadModel('Tpagamento'); // THIS IS WORKING
    $tpagamentos = $this->Tpagamento->read(null,'5');
    $this->set('tpagamentos',$tpagamentos);

}

OrdemServico 模型:

class Ordemservico extends AppModel {

    public $displayField = 'cliente_id';

    public $belongsTo = array(      
        'Tpagamento' => array(
            'className' => 'Tpagamento',
            'foreignKey' => 'tpagamento_id',
        ),
        'Parcelamento' => array(
            'className' => 'Parcelamento',
            'foreignKey' => 'parcelamento_id',
        ),
    );

}

Tpagamento 模型:

class Tpagamento extends AppModel {

    public $useTable = 'tpagamentos';

    public $displayField = 'nome';

    public $hasMany = array(
        'Ordemservico' => array(
            'className' => 'Ordemservico',
            'foreignKey' => 'tpagamento_id',
        ),

    );



}

包裹模型:

class Parcelamento extends AppModel {

    public $displayField = 'nome';

    public $hasMany = array(
        'Ordemservico' => array(
            'className' => 'Ordemservico',
            'foreignKey' => 'parcelamento_id',
        ),

    );



}

生成的 SQL DUMP:

4   SELECT `Parcelamento`.`id`, `Parcelamento`.`nome` FROM `tereza`.`parcelamentos` AS `Parcelamento` WHERE 1 = 1       5   5   0

5   SELECT `Tpagamento`.`id`, `Tpagamento`.`nome` FROM `tereza`.`tpagamentos` AS `Tpagamento` WHERE 1 = 1       4   4   0

6   SELECT `Tpagamento`.`id`, `Tpagamento`.`nome`, `Tpagamento`.`created`, `Tpagamento`.`modified` FROM `tereza`.`tpagamentos` AS `Tpagamento` WHERE `Tpagamento`.`id` = 5 LIMIT 1      1   1   0

【问题讨论】:

  • 显示生成的 SQL 查询
  • 3 个重要问题:为什么我可以列出如果 displayField = id?如果 displayField = 'nome' 为什么我不能列出?为什么我不能列出,但我可以读取(null,$id)?
  • 我解决了我的问题:在 database.php 中我设置了 'encoding' => 'utf8'。

标签: cakephp model cakephp-2.0


【解决方案1】:

在我设置的 Config/database.php 中:'encoding' => 'utf8'

在一张桌子而不是另一张桌子上工作的原因是因为在一张桌子上(tpagamentos)我有特殊字符而在另一张桌子上没有(parcelamentos)。

【讨论】:

    【解决方案2】:

    要在列表中显示两个字段,请使用以下语法:

    $this->Parcelamento->find('list', array('fields' => array('id', 'other_field_name')));
    

    【讨论】:

    • 在我设置的 Config/database.php 中:'encoding' => 'utf8' 在一个表而不是另一个表中工作的原因是因为在一个表(tpagamentos)中我有特殊字符并且在另一个不是(parcelamentos)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多