【问题标题】:how to insert data in foreign key along with other data in zend framework如何在外键中插入数据以及zend框架中的其他数据
【发布时间】:2012-08-15 17:49:05
【问题描述】:

将数据插入 MySQL 数据库时出现问题。出现以下错误:

SQLSTATE[HY000]: 一般错误: 1452 无法添加或更新子行: 外键约束失败 (login.employees, CONSTRAINT employees_ibfk_1 FOREIGN KEY (u_id) REFERENCES users ( id) 更新级联删除级联)

我有一张名为employees的表:

+-------------+
|  employees  |
+-------------+
| id          |
| u_id        |  foreign key of table `user` field `id`
| firstname   |
| lastname    |
| city        |
| designation |
+-------------+

我的表单代码:

class Application_Form_Login extends Zend_Form
{
public function init()
{
    $this->setName('Login');
    $this->setMethod('post');

    $username = $this->createElement('text','username');
    $username->setLabel('Username: *')
            ->setRequired(true)
            ->setFilters(array('StringTrim','StringToLower'))
            ->getValidators(array('stringLength',false,array(0,50)));

    $password = $this->createElement('password','password');
    $password->setLabel('Password: *')
            ->setRequired(true)
            ->setFilters(array('StringTrim'))
            ->getValidators(array('stringLength',false,array(0,50)));

    $submit = $this->createElement('submit','Sign in');
    $submit->setLabel('Sign in')
            ->setIgnore(true);

    $this->addElements(array($username,
                        $password,
                        $submit,));
}
}

这是我的桌子模型:

class Application_Model_Dbtables_Employees extends Zend_Db_Table
{
protected $_name = 'employees';
}  

这是我的控制器:

class EmployeeController extends Zend_Controller_Action
{

public function init()
{
    /* Initialize action controller here */
}

public function indexAction()
{
    // action body
}

public function addAction()
{
    $emp = new Application_Model_Dbtables_Employees();
    $form = new Application_Form_employee();
    $this->view->form = $form;

    if($this->getRequest()->isPost())
    {
        $formdata = $this->_request->getPost();

          if($form->isValid($formdata))
          {
            unset($formdata['addemployee']);
            $emp->insert($formdata);
            $this->_redirect('Auth/home');
          }
          else
          {
            $this->view->errMsg = "Cound not able to insert data";
          }
    }
}
}

还有我的视图文件:

<?php
if(isset($this->errMsg)) {
echo $this->errMsg;
}
?>

<?php 
 echo $this->form;
?>

我的代码员工

<?php
class Application_Form_employee extends Zend_Form
{
public function init()
{
    $this->setMethod('post');
    $this->setName('addemployee');

    $firstname = $this->createElement('text', 'firstname');
    $firstname->setLabel('Firstname: *')
                ->setRequired(true)
                ->setFilters(array('StringToLower'));

    $lastname = $this->createElement('text', 'lastname');
    $lastname->setLabel('Lastname: *')
                ->setRequired(true)
                ->setFilters(array('StringToLower'));

    $city = $this->createElement('select', 'city');
    $city->setLabel('city: ')
        ->setRequired(true)
        ->setMultiOptions(array(
                '' => '---select city---',
                'ahmedabad' => 'Ahmedabad',
                'Vadodara' => 'Vadodara',
                'Anand' => 'Anand',
                'Surat' => 'Surat'));
    $designation = $this->createElement('select', 'designation');
    $designation->setLabel('Designation: ')
                ->setMultiOptions(array(
                        'java' => 'Java developer',
                        'php' => 'PHP developer',
                        'unix' => 'Unix developer'));

    $submit = $this->createElement('submit', 'addemployee');
    $submit->setLabel('Add Employee')
            ->setIgnore(true);

    $this->addElements(array(
            $firstname,
            $lastname,
            $city,
            $designation,
            $submit,));
}
}
?>

【问题讨论】:

  • 请发布 Application_Form_employee 代码
  • 添加了员工表单代码。
  • 在您的控制器中,您不会在 u_id 字段中存储任何内容。那个领域应该是什么?
  • 当任何用户登录时。。 .thay 有 id(u_id)。并添加员工(在员工表中)。因为用户可以添加超过 1 名员工,并且许多员工只有一张表。所以其他用户也添加了他们的员工。登录后,我将数据存储在 zend_auth_storage_session() 中并获取用户的行和 id。

标签: php zend-framework


【解决方案1】:

这是我能想到的一种让它发挥作用的方法。基本上,当在员工表中插入新条目时,您有一个限制,即数据库需要相应的外键 (u_id)。您传递了表单数据,但没有传递 u_id。因此,在 Application_Form_employee 表单中,您可以添加一个隐藏字段来保存插入记录的用户 ID:

$uId = $this->createElement('hidden', 'u_id');
$uId->setValue(Zend_Auth::getInstance()->getIdentity()->id);
//And when you add elements to your form, add this field
    $this->addElements(array(
            $firstname,
            $lastname,
            $city,
            $designation,
            $uId,
            $submit,));

我认为这可以解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多