doctrine 2缺少主键的值

时间:2014-07-16 18:30:00

标签: php doctrine-orm

这个问题已经解决了,但也许其他人也可以找到一些帮助。

我在zend框架2和doctrine 2中创建了一个项目。在我的项目中,我有系统用户和项目。一个项目可以有多个具有多个操作的服务器。每个操作都由用户进行,因此所有操作都链接到用户。一切正常,直到我尝试列出所有动作。比我在用户链接上出错并且错误说:Sysusers \ Entity \ SystemUser上的主键ID缺失值

我前一段时间也有这个问题,我发现@ORM \ JoinColumn在哪里翻转。但现在代码中的一切似乎都没问题。

如果有人能指出我正确的方向,那就太好了。提前谢谢!

这是我的系统用户实体:

<?php

/**
 * Auto generated by MySQL Workbench Schema Exporter.
 * Version 2.1.3-dev (doctrine2-zf2inputfilterannotation) on 2014-07-15
 * 14:04:16.
 * Goto https://github.com/johmue/mysql-workbench-schema-exporter for more
 * information.
 */

namespace Sysusers\Entity;

use Doctrine\ORM\Mapping as ORM;
use Zend\Crypt\Password\Bcrypt;
use Dashboard\Entity\ProjectAction;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Sysusers\Entity\SystemUser
 *
 * @ORM\Entity(repositoryClass="SystemUserRepository")
 * @ORM\Table(name="system_users", uniqueConstraints={@ORM\UniqueConstraint(name="email_UNIQUE", columns={"email"})})
 */
class SystemUser implements \ZfcUser\Entity\UserInterface
{
    /**
     * Instance of InputFilterInterface.
     *
     * @var InputFilter
     */
    private $inputFilter;

    /**
     * @ORM\Id
     * @ORM\Column(type="integer", options={"unsigned"=true})
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=45)
     */
    protected $first_name;

    /**
     * @ORM\Column(type="string", length=45)
     */
    protected $last_name;

    /**
     * @ORM\Column(type="string", length=45)
     */
    protected $email;

    /**
     * @ORM\Column(type="string", length=250)
     */
    protected $password;

    /**
     * @ORM\OneToMany(targetEntity="Dashboard\Entity\ProjectAction", mappedBy="systemUser")
     * @ORM\JoinColumn(name="system_users_id", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $projectActions;

    public function __construct()
    {
        $this->projectActions = new ArrayCollection();
    }

    /**
     * Set the value of id.
     *
     * @param integer $id
     * @return \Sysusers\Entity\SystemUser
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * Get the value of id.
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set the value of first_name.
     *
     * @param string $first_name
     * @return \Sysusers\Entity\SystemUser
     */
    public function setFirstName($first_name)
    {
        $this->first_name = $first_name;

        return $this;
    }

    /**
     * Get the value of first_name.
     *
     * @return string
     */
    public function getFirstName()
    {
        return $this->first_name;
    }

    /**
     * Set the value of last_name.
     *
     * @param string $last_name
     * @return \Sysusers\Entity\SystemUser
     */
    public function setLastName($last_name)
    {
        $this->last_name = $last_name;

        return $this;
    }

    /**
     * Get the value of last_name.
     *
     * @return string
     */
    public function getLastName()
    {
        return $this->last_name;
    }

    /**
     * Set the value of email.
     *
     * @param string $email
     * @return \Sysusers\Entity\SystemUser
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get the value of email.
     *
     * @return string
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set the value of password.
     *
     * @param string $password
     * @return \Sysusers\Entity\SystemUser
     */
    public function setPassword($password, $hash = true)
    {
        if ($hash) {
            $crypt    = new Bcrypt();
            $password = $crypt->create($password);
        }

        $this->password = $password;

        return $this;
    }

    /**
     * Get the value of password.
     *
     * @return string
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * Add ProjectAction entity to collection (one to many).
     *
     * @param \Dashboard\Entity\ProjectAction $projectAction
     * @return \Sysusers\Entity\SystemUser
     */
    public function addProjectAction(ProjectAction $projectAction)
    {
        $this->projectActions[] = $projectAction;

        return $this;
    }

    /**
     * Remove ProjectAction entity from collection (one to many).
     *
     * @param \Dashboard\Entity\ProjectAction $projectAction
     * @return \Sysusers\Entity\SystemUser
     */
    public function removeProjectAction(ProjectAction $projectAction)
    {
        $this->projectActions->removeElement($projectAction);

        return $this;
    }

    /**
     * Get ProjectAction entity collection (one to many).
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getProjectActions()
    {
        return $this->projectActions;
    }

    /**
     * Populate entity with the given data.
     * The set* method will be used to set the data.
     *
     * @param array $data
     * @return boolean
     */
    public function populate(array $data = array())
    {
        foreach ($data as $field => $value) {
            $setter = sprintf('set%s', ucfirst(
                str_replace(' ', '', ucwords(str_replace('_', ' ', $field)))
            ));
            if (method_exists($this, $setter)) {
                $this->{$setter}($value);
            }
        }

        return true;
    }

    /**
     * Return a array with all fields and data.
     * Default the relations will be ignored.
     * 
     * @param array $fields
     * @return array
     */
    public function getArrayCopy(array $fields = array())
    {
        $dataFields = array('id', 'first_name', 'last_name', 'email', 'password');
        $relationFields = array();
        $copiedFields = array();
        foreach ($relationFields as $relationField) {
            $map = null;
            if (array_key_exists($relationField, $fields)) {
                $map = $fields[$relationField];
                $fields[] = $relationField;
                unset($fields[$relationField]);
            }
            if (!in_array($relationField, $fields)) {
                continue;
            }
            $getter = sprintf('get%s', ucfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $relationField)))));
            $relationEntity = $this->{$getter}();
            $copiedFields[$relationField] = (!is_null($map))
                ? $relationEntity->getArrayCopy($map)
                : $relationEntity->getArrayCopy();
            $fields = array_diff($fields, array($relationField));
        }
        foreach ($dataFields as $dataField) {
            if (!in_array($dataField, $fields) && !empty($fields)) {
                continue;
            }
            $getter = sprintf('get%s', ucfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $dataField)))));
            $copiedFields[$dataField] = $this->{$getter}();
        }

        return $copiedFields;
    }

    public function __sleep()
    {
        return array('id', 'first_name', 'last_name', 'email', 'password');
    }

    public function getUsername()
    {
        return $this->getFirstName() . ' ' . $this->getLastName();
    }

    public function getDisplayName()
    {
        return $this->getFirstName() . ' ' . $this->getLastName();
    }

    public function getState()
    {
        return false;
    }
}

这是我的项目实体:

<?php

/**
 * Auto generated by MySQL Workbench Schema Exporter.
 * Version 2.1.3-dev (doctrine2-zf2inputfilterannotation) on 2014-06-28
 * 14:43:09.
 * Goto https://github.com/johmue/mysql-workbench-schema-exporter for more
 * information.
 */

namespace Dashboard\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Dashboard\Entity\Project
 *
 * @ORM\Entity(repositoryClass="ProjectRepository")
 * @ORM\Table(name="projects")
 */
class Project
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer", options={"unsigned"=true})
     */
    protected $id;

    /**
     * @ORM\Column(nullable=true)
     */
    protected $last_action;

    /**
     * @ORM\OneToMany(targetEntity="Server", mappedBy="project")
     * @ORM\JoinColumn(name="projects_id", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $servers;

    public function __construct()
    {
        $this->servers = new ArrayCollection();
    }

    /**
     * Set the value of id.
     *
     * @param integer $id
     * @return \Dashboard\Entity\Project
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * Get the value of id.
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set the value of last_action.
     *
     * @param  $last_action
     * @return \Dashboard\Entity\Project
     */
    public function setLastAction($last_action)
    {
        $this->last_action = $last_action;

        return $this;
    }

    /**
     * Get the value of last_action.
     *
     * @return 
     */
    public function getLastAction()
    {
        return $this->last_action;
    }

    /**
     * Add Server entity to collection (one to many).
     *
     * @param \Dashboard\Entity\Server $server
     * @return \Dashboard\Entity\Project
     */
    public function addServer(Server $server)
    {
        $this->servers[] = $server;

        return $this;
    }

    /**
     * Remove Server entity from collection (one to many).
     *
     * @param \Dashboard\Entity\Server $server
     * @return \Dashboard\Entity\Project
     */
    public function removeServer(Server $server)
    {
        $this->servers->removeElement($server);

        return $this;
    }

    /**
     * Get Server entity collection (one to many).
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getServers()
    {
        return $this->servers;
    }

    /**
     * Populate entity with the given data.
     * The set* method will be used to set the data.
     *
     * @param array $data
     * @return boolean
     */
    public function populate(array $data = array())
    {
        foreach ($data as $field => $value) {
            $setter = sprintf('set%s', ucfirst(
                str_replace(' ', '', ucwords(str_replace('_', ' ', $field)))
            ));
            if (method_exists($this, $setter)) {
                $this->{$setter}($value);
            }
        }

        return true;
    }

    /**
     * Return a array with all fields and data.
     * Default the relations will be ignored.
     * 
     * @param array $fields
     * @return array
     */
    public function getArrayCopy(array $fields = array())
    {
        $dataFields = array('id', 'last_action');
        $relationFields = array();
        $copiedFields = array();
        foreach ($relationFields as $relationField) {
            $map = null;
            if (array_key_exists($relationField, $fields)) {
                $map = $fields[$relationField];
                $fields[] = $relationField;
                unset($fields[$relationField]);
            }
            if (!in_array($relationField, $fields)) {
                continue;
            }
            $getter = sprintf('get%s', ucfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $relationField)))));
            $relationEntity = $this->{$getter}();
            $copiedFields[$relationField] = (!is_null($map))
                ? $relationEntity->getArrayCopy($map)
                : $relationEntity->getArrayCopy();
            $fields = array_diff($fields, array($relationField));
        }
        foreach ($dataFields as $dataField) {
            if (!in_array($dataField, $fields) && !empty($fields)) {
                continue;
            }
            $getter = sprintf('get%s', ucfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $dataField)))));
            $copiedFields[$dataField] = $this->{$getter}();
        }

        return $copiedFields;
    }

    public function __sleep()
    {
        return array('id', 'last_action');
    }
}

这是我的项目行动实体:

<?php

/**
 * Auto generated by MySQL Workbench Schema Exporter.
 * Version 2.1.3-dev (doctrine2-zf2inputfilterannotation) on 2014-07-15
 * 14:04:16.
 * Goto https://github.com/johmue/mysql-workbench-schema-exporter for more
 * information.
 */

namespace Dashboard\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Dashboard\Entity\ProjectAction
 *
 * @ORM\Entity(repositoryClass="ProjectActionRepository")
 * @ORM\Table(name="project_actions", indexes={@ORM\Index(name="fk_project_actions_system_users_idx", columns={"system_users_id"}), @ORM\Index(name="fk_project_actions_projects1_idx", columns={"projects_id"}), @ORM\Index(name="fk_project_actions_servers1_idx", columns={"servers_id"})})
 */
class ProjectAction
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer", options={"unsigned"=true})
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column()
     */
    protected $date_created;

    /**
     * @ORM\Column(type="integer", options={"unsigned"=true})
     */
    protected $system_users_id;

    /**
     * @ORM\Column(type="integer", options={"unsigned"=true})
     */
    protected $servers_id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    protected $commit_key;

    /**
     * @ORM\Column(type="integer", nullable=true, options={"unsigned"=true})
     */
    protected $creates;

    /**
     * @ORM\Column(type="integer", nullable=true, options={"unsigned"=true})
     */
    protected $updates;

    /**
     * @ORM\Column(type="integer", nullable=true, options={"unsigned"=true})
     */
    protected $deletes;

    /**
     * @ORM\OneToMany(targetEntity="ActionFile", mappedBy="projectAction")
     * @ORM\JoinColumn(name="project_actions_id", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $actionFiles;

    /**
     * @ORM\ManyToOne(targetEntity="Sysusers\Entity\SystemUser", inversedBy="projectActions")
     * @ORM\JoinColumn(name="id", referencedColumnName="system_users_id", onDelete="CASCADE")
     */
    protected $systemUser;

    /**
     * @ORM\ManyToOne(targetEntity="Server", inversedBy="projectActions")
     * @ORM\JoinColumn(name="id", referencedColumnName="servers_id")
     */
    protected $server;

    public function __construct()
    {
        $this->actionFiles = new ArrayCollection();
    }

    /**
     * Set the value of id.
     *
     * @param integer $id
     * @return \Dashboard\Entity\ProjectAction
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * Get the value of id.
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set the value of date_created.
     *
     * @param  $date_created
     * @return \Dashboard\Entity\ProjectAction
     */
    public function setDateCreated($date_created)
    {
        $this->date_created = $date_created;

        return $this;
    }

    /**
     * Get the value of date_created.
     *
     * @return 
     */
    public function getDateCreated()
    {
        return $this->date_created;
    }

    /**
     * Set the value of system_users_id.
     *
     * @param integer $system_users_id
     * @return \Dashboard\Entity\ProjectAction
     */
    public function setSystemUsersId($system_users_id)
    {
        $this->system_users_id = $system_users_id;

        return $this;
    }

    /**
     * Get the value of system_users_id.
     *
     * @return integer
     */
    public function getSystemUsersId()
    {
        return $this->system_users_id;
    }

    /**
     * Get the value of projects_id.
     *
     * @return integer
     */
    public function getProjectsId()
    {
        return $this->projects_id;
    }

    /**
     * Set the value of servers_id.
     *
     * @param integer $servers_id
     * @return \Dashboard\Entity\ProjectAction
     */
    public function setServersId($servers_id)
    {
        $this->servers_id = $servers_id;

        return $this;
    }

    /**
     * Get the value of servers_id.
     *
     * @return integer
     */
    public function getServersId()
    {
        return $this->servers_id;
    }

    /**
     * Set the value of commit_key.
     *
     * @param string $commit_key
     * @return \Dashboard\Entity\ProjectAction
     */
    public function setCommitKey($commit_key)
    {
        $this->commit_key = $commit_key;

        return $this;
    }

    /**
     * Get the value of commit_key.
     *
     * @return string
     */
    public function getCommitKey()
    {
        return $this->commit_key;
    }

    /**
     * Set the value of creates.
     *
     * @param integer $creates
     * @return \Dashboard\Entity\ProjectAction
     */
    public function setCreates($creates)
    {
        $this->creates = $creates;

        return $this;
    }

    /**
     * Get the value of creates.
     *
     * @return integer
     */
    public function getCreates()
    {
        return $this->creates;
    }

    /**
     * Set the value of updates.
     *
     * @param integer $updates
     * @return \Dashboard\Entity\ProjectAction
     */
    public function setUpdates($updates)
    {
        $this->updates = $updates;

        return $this;
    }

    /**
     * Get the value of updates.
     *
     * @return integer
     */
    public function getUpdates()
    {
        return $this->updates;
    }

    /**
     * Set the value of deletes.
     *
     * @param integer $deletes
     * @return \Dashboard\Entity\ProjectAction
     */
    public function setDeletes($deletes)
    {
        $this->deletes = $deletes;

        return $this;
    }

    /**
     * Get the value of deletes.
     *
     * @return integer
     */
    public function getDeletes()
    {
        return $this->deletes;
    }

    /**
     * Add ActionFile entity to collection (one to many).
     *
     * @param \Dashboard\Entity\ActionFile $actionFile
     * @return \Dashboard\Entity\ProjectAction
     */
    public function addActionFile(ActionFile $actionFile)
    {
        $this->actionFiles[] = $actionFile;

        return $this;
    }

    /**
     * Remove ActionFile entity from collection (one to many).
     *
     * @param \Dashboard\Entity\ActionFile $actionFile
     * @return \Dashboard\Entity\ProjectAction
     */
    public function removeActionFile(ActionFile $actionFile)
    {
        $this->actionFiles->removeElement($actionFile);

        return $this;
    }

    /**
     * Get ActionFile entity collection (one to many).
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getActionFiles()
    {
        return $this->actionFiles;
    }

    /**
     * Set SystemUser entity (many to one).
     *
     * @param \Sysusers\Entity\SystemUser $systemUser
     * @return \Dashboard\Entity\ProjectAction
     */
    public function setSystemUser(SystemUser $systemUser = null)
    {
        $this->systemUser = $systemUser;

        return $this;
    }

    /**
     * Get SystemUser entity (many to one).
     *
     * @return \Sysusers\Entity\SystemUser
     */
    public function getSystemUser()
    {
        return $this->systemUser;
    }

    /**
     * Set Server entity (many to one).
     *
     * @param \Dashboard\Entity\Server $server
     * @return \Dashboard\Entity\ProjectAction
     */
    public function setServer(Server $server = null)
    {
        $this->server = $server;

        return $this;
    }

    /**
     * Get Server entity (many to one).
     *
     * @return \Dashboard\Entity\Server
     */
    public function getServer()
    {
        return $this->server;
    }

    /**
     * Populate entity with the given data.
     * The set* method will be used to set the data.
     *
     * @param array $data
     * @return boolean
     */
    public function populate(array $data = array())
    {
        foreach ($data as $field => $value) {
            $setter = sprintf('set%s', ucfirst(
                str_replace(' ', '', ucwords(str_replace('_', ' ', $field)))
            ));
            if (method_exists($this, $setter)) {
                $this->{$setter}($value);
            }
        }

        return true;
    }

    /**
     * Return a array with all fields and data.
     * Default the relations will be ignored.
     * 
     * @param array $fields
     * @return array
     */
    public function getArrayCopy(array $fields = array())
    {
        $dataFields = array('id', 'date_created', 'system_users_id', 'servers_id', 'commit_key', 'creates', 'updates', 'deletes');
        $relationFields = array('systemUser', 'project', 'server');
        $copiedFields = array();
        foreach ($relationFields as $relationField) {
            $map = null;
            if (array_key_exists($relationField, $fields)) {
                $map = $fields[$relationField];
                $fields[] = $relationField;
                unset($fields[$relationField]);
            }
            if (!in_array($relationField, $fields)) {
                continue;
            }
            $getter = sprintf('get%s', ucfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $relationField)))));
            $relationEntity = $this->{$getter}();
            $copiedFields[$relationField] = (!is_null($map))
                ? $relationEntity->getArrayCopy($map)
                : $relationEntity->getArrayCopy();
            $fields = array_diff($fields, array($relationField));
        }
        foreach ($dataFields as $dataField) {
            if (!in_array($dataField, $fields) && !empty($fields)) {
                continue;
            }
            $getter = sprintf('get%s', ucfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $dataField)))));
            $copiedFields[$dataField] = $this->{$getter}();
        }

        return $copiedFields;
    }

    public function __sleep()
    {
        return array('id', 'date_created', 'system_users_id', 'servers_id', 'commit_key', 'creates', 'updates', 'deletes');
    }
}

0 个答案:

没有答案