【问题标题】:Why does cakephp not insert a field in a mysql table?为什么 cakephp 不在 mysql 表中插入字段?
【发布时间】:2013-01-01 08:10:05
【问题描述】:

我在使用 CakePHP 2.2.4 时遇到了一个非常奇怪的问题,让我解释一下发生了什么。

我有这些 mysql 表:

潜在客户

+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| user_id    | int(11)      | YES  | MUL | NULL    |                |
| date       | datetime     | YES  |     | NULL    |                |
| ip         | varchar(45)  | YES  |     | NULL    |                |
| service_id | int(11)      | YES  | MUL | NULL    |                |
| location   | varchar(255) | YES  |     | NULL    |                |
| message    | text         | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

用户

+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int(11)      | NO   | PRI | NULL    | auto_increment |
| email   | varchar(200) | YES  |     | NULL    |                |
| pwd     | varchar(200) | YES  |     | NULL    |                |
| role_id | int(11)      | YES  | MUL | NULL    |                |
| active  | tinyint(4)   | YES  |     | 0       |                |
+---------+--------------+------+-----+---------+----------------+

个人资料

+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int(11)      | NO   | PRI | NULL    | auto_increment |
| user_id | int(11)      | YES  | MUL | NULL    |                |
| name    | varchar(120) | YES  |     | NULL    |                |
| surname | varchar(120) | YES  |     | NULL    |                |
| company | varchar(200) | YES  |     | NULL    |                |
| vat     | varchar(60)  | YES  |     | NULL    |                |
+---------+--------------+------+-----+---------+----------------+

地址

+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| profile_id | int(11)      | YES  | MUL | NULL    |                |
| address    | varchar(255) | YES  |     | NULL    |                |
| city       | varchar(120) | YES  |     | NULL    |                |
| zip_code   | char(5)      | YES  |     | NULL    |                |
| nation     | varchar(120) | YES  |     | NULL    |                |
| telephone  | varchar(200) | YES  |     | NULL    |                |
| mobile     | varchar(200) | YES  |     | NULL    |                |
| fax        | varchar(200) | YES  |     | NULL    |                |
| email      | varchar(200) | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

角色

+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(100) | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+

服务

+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(255) | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+

以下是我使用的所有 CakePhp 模型(带有关联):

Lead.php

class Lead extends AppModel {

    public $name = 'Lead';

    /* Relazioni */      
    public $belongsTo = array('User', 'Service');
}

User.php

class User extends AppModel {

    public $name = 'User';

    /* Relazioni */
    public $hasOne = 'Profile';

    public $belongsTo = 'Role';

    public $hasMany = array(
        'Lead' => array(
            'className' => 'Lead'
        )
    );  

}

Profile.php

class Profile extends AppModel {

    public $name = 'Profile';

    /* Relazioni */
    public $belongsTo = 'User';

    public $hasMany = array(
        'Address' => array(
            'className' => 'Address'
        )
    );  

}

地址.php

class Address extends AppModel {

    public $name = 'Address';

    /* Relazioni */        
    public $belongsTo = 'Profile';

}

Service.php

class Service extends AppModel {

    public $name = 'Service';

}

我有一个必须保存在多个 mysql 表中的表单。

这是我用来起草表格的代码:

<?php

echo $this->Form->create(null, array(
                            'inputDefaults' => array(
                                'error' => false
                            )
                        )
                ); //create(false) per il form senza il modello

echo $this->Form->input('User.Profile.name', array(
                            'label' => array(   
                                'class' => 'name-form',                                                                                                                                         
                                'text'  => 'Nome:'                                                                      
                            ),                                                              
                            'style' => 'width:238px;',
                            'div'   => 'field'
                        )
               );                               

echo $this->Form->input('User.Profile.surname', array(
                            'label' => array(   
                                'class' => 'name-form',                                                                                                                                         
                                'text'  => 'Cognome:'                                                                       
                            ),                                                              
                            'style' => 'width:238px;',
                            'div'   => 'field'
                        )
               );                                   

?>

<div class="clear"></div>

<?php

echo $this->Form->input('User.email', array(
                            'label' => array(                                                                       
                                'class' => 'name-form',
                                'text'  => 'Email:'
                            ),                                                              
                            'div'   => 'field', 
                            'class' => 'input-xlarge'
                        )
               );

echo $this->Form->input('User.Profile.Address.telephone', array(
                            'label' => array(   
                                'class' => 'name-form',                                                                                                                                         
                                'text'  => 'Telefono:'                                                                      
                            ),                                                          
                            'div'   => 'field'
                        )
               );                          
?>

<div class="clear"></div>


<?php                      

echo $this->Form->input('Lead.service_id',
                    array(
                        'label' => array(                                                                       
                            'class' => 'name-form',
                            'text'  => 'Servizio richiesto:'
                        ),                                                              
                        'div'   => 'field',
                        'type'  => 'select',                                                                
                        'options' => $services                                      
                    )
);                              

echo $this->Form->input('Lead.location', array(
                            'label' => array(                                                                       
                                'class' => 'name-form',
                                'text'  => 'Vicino a:'
                            ),                                                              
                            'div'   => 'field', 
                            'class' => 'input-xlarge'
                        )
               );                               

?>

<div class="clear"></div>


<?php

echo $this->Form->input('Lead.message', array(
                            'label' => array(                                                                       
                                'class' => 'message-form',
                                'text'  => 'Richiesta:'
                            ),                                                              
                            'div'   => 'field', 
                            'class' => 'form-field',
                            'type'  => 'textarea'
                        )
               );                                                               

echo $this->Form->button('Invia Richiesta', array(
                                        'type'  => 'submit',
                                        'class' => 'submit-btn less-margin'
                                    )
                );

echo $this->Form->end();

?>

这是保存数据的控制器(PagesController)的动作:

public function home() 
{       
   if ($this->request->is('post'))
   {
    $this->loadModel('Lead');

    $this->request->data['Lead']['ip']   = $_SERVER['REMOTE_ADDR'];
    $this->request->data['Lead']['date'] = date('Y-m-d H:i:s'); 
    $this->request->data['User']['role_id'] = 3;    

    $this->Lead->set($this->request->data);

    debug($this->request->data);

    if ($this->Lead->validates()) 
    {
        // Dati nel modulo corretti
        debug($this->Lead->saveAll($this->request->data, array('deep' => true)));
    } 
    else 
    {
        // Errore nella validazione
        $this->Session->setFlash('Completare correttamente il form', 'default', array('class' => 'errore'), 'lead');

        $errors = $this->Lead->validationErrors;                
        debug($errors);
    }   
}

$this->loadModel('Service');

$this->set('services', $this->Service->find('list'));               

}

这是从 FORM 生成的输出:

array(
    'User' => array(
        'Profile' => array(
            'name' => 'The Name',
            'surname' => 'The Surname',
            'Address' => array(
                'telephone' => 'The Telephone'
            )
        ),
        'email' => 'the@email.com',
        'role_id' => (int) 3
    ),
    'Lead' => array(
        'service_id' => '1',
        'location' => 'The location',
        'message' => 'This is the request',
        'ip' => '127.0.0.1',
        'date' => '2013-01-01 08:07:42'
    )
)

问题:

一切正常,除了一件事...saveAll() 正确保存所有数据(在每个模型中),但在地址表中我只看到 CakePhp 插入 profile_id,只有这个字段,但是正如您在从 FORM 生成的数组中看到的那样,我也有电话!

为什么 cakephp 不也插入 telephone 字段?

谢谢

【问题讨论】:

  • 您需要查看book.cakephp.org/2.0/en/models/…。特别是说:重要的是要意识到保存模型数据应该始终由相应的 CakePHP 模型完成
  • @TimJoyce 对不起,我不明白你......你是在告诉我,如果我有 3 组字段(对于组,我的意思是......与不同模型相关的字段),我必须一遍又一遍地做 Model1->save() ... Model2->save() --- Model3->save() 吗?你是这个意思吗?
  • 是的,这个例子在这里:book.cakephp.org/2.0/en/models/… 应该让你走上保存相关模型数据的正确路径。
  • @TimJoyce 在我遵循此方法之前,但如果我必须为每个模型使用 ->save(),我什么时候需要使用 saveAll() ?
  • @TimJoyce 又一次......这不是太多余了吗?因为目前一切正常,除了(电话),但 cakephp 写在地址表中,因为我看到了正确的 profile_id

标签: php cakephp cakephp-2.0


【解决方案1】:

您正在编写代码,因此它不起作用。

蛋糕文档中没有任何地方说使用以下代码:

不正确

$this->Form->input('User.Profile.surname');
$this->Form->input('User.Profile.Address.telephone');

你不能只是把单词串在一起,然后寄希望于最好的结果。您可以使用:

正确

$this->Form->input('field');
$this->Form->input('Model.field');
$this->Form->input('Model.n.field');

没有其他有效的方法可以输入字段。

请阅读docs

$this->Form->input('Profile.surname');

【讨论】:

  • @dogmatic69 我不明白什么“格式”是正确的。我已经使用了这样的格式:'User.Profile.Address.telephone' 这是错误的吗?
  • @Dail 是的,这就是他的观点。它应该只是Address.telephone。关系将处理其余部分。
  • @Oldskool 'Address.telephone' 不起作用。我尝试了它而不是'User.Profile.Address.telephone'....如果我这样做,CakePHP 也不会在地址表中保存 profile_id
【解决方案2】:

saveAll 函数只是 saveMany 和 saveAssociated 方法的包装。”

尝试改用saveAssociated,以便强制使用saveAssociated 方法。仍然通过array('deep'=&gt;true);。您的输出格式的方式,它可能会尝试自动使用saveMany

此外,如果您对模型中的 telephone 字段进行了验证,它将失败,因为您有一个不包含任何数字的字符串。 The Telephone。或者,您可以传递'validate' =&gt; false的选项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多