学说一对一空结果

时间:2017-01-01 20:28:39

标签: php symfony doctrine-orm

用户实体:

<?php

namespace App\Entity;

use App\Entity;
use Doctrine\ORM\Mapping;

/**
 * @Entity
 * @Table(name="users", options={"collate":"utf8_general_ci", "charset":"utf8", "engine":"MyISAM"})
 */
class Users extends Entity {

    /**
     * One Users has One UserInformation. Inversed here.
     * @OneToOne(targetEntity="UserInformation",mappedBy="users")
     */
    protected $userInformation;

    /**
     * @Column(type="string", length=50)
     * @var string
     */
    protected $email;

    /**
     * @Column(type="string", length=50)
     * @var string
     */
    protected $encrypted_password;

    /**
     * @Column(type="string", length=10)
     * @var string
     */
    protected $salt;

    /**
     * @Column(type="smallint", options={"default":"0","comment":"0 : Doctor, 1 : Assistant, 2 : Student"}))
     * @var smallint
     */
    protected $type;

    /**
     * @Column(type="string", length=150)
     * @var string
     */
    protected $sef_link;

    /**
     * @return mixed
     */
    public function getUserInformation() {
        return $this->userInformation;
    }

    /**
     * @param mixed $userInformation
     */
    public function setUserInformation($userInformation) {
        $this->userInformation = $userInformation;
    }

    /**
     * @return string
     */
    public function getEmail() {
        return $this->email;
    }

    /**
     * @param string $email
     */
    public function setEmail($email) {
        $this->email = $email;
    }

    /**
     * @return string
     */
    public function getEncryptedPassword() {
        return $this->encrypted_password;
    }

    /**
     * @param string $encrypted_password
     */
    public function setEncryptedPassword($encrypted_password) {
        $this->encrypted_password = $encrypted_password;
    }

    /**
     * @return string
     */
    public function getSalt() {
        return $this->salt;
    }

    /**
     * @param string $salt
     */
    public function setSalt($salt) {
        $this->salt = $salt;
    }

    /**
     * @return integer
     */
    public function getType() {
        return $this->type;
    }

    /**
     * @param integer $type
     */
    public function setType($type) {
        $this->type = $type;
    }

    /**
     * @return string
     */
    public function getSefLink() {
        return $this->sef_link;
    }

    /**
     * @param string $sef_link
     */
    public function setSefLink($sef_link) {
        $this->sef_link = $sef_link;
    }
}

UserInformation实体:

<?php

namespace App\Entity;

use App\Entity;
use Doctrine\ORM\Mapping;

/**
 * @Entity
 * @Table(name="user_information", options={"collate":"utf8_general_ci", "charset":"utf8", "engine":"MyISAM"})
 */
class UserInformation extends Entity {

    /**
     * One UserInformation has One Uses. Mapped here.
     * @OneToOne(targetEntity="Users")
     * @JoinColumn(name="uid", referencedColumnName="id")
     */
    protected $users;

    /**
     * @Column(type="string", length=50)
     * @var string
     */
    protected $title;

    /**
     * @Column(type="string", length=50)
     * @var string
     */
    protected $name;

    /**
     * @Column(type="string", length=50)
     * @var string
     */
    protected $surname;

    /**
     * @Column(type="date", nullable=true)
     * @var date
     */
    protected $born;

    /**
     * @Column(type="smallint", options={"default" : "0", "comment":"0 : Female, 1 : Male"}))
     * @var smallint
     */
    protected $gender;

    /**
     * @Column(type="text", length=65532, nullable=true)
     * @var text
     */
    protected $address;

    /**
     * @Column(type="integer", nullable=true)
     * @var integer
     */
    protected $country_id;

    /**
     * @Column(type="text", length=65532, nullable=true)
     * @var text
     */
    protected $about;

    /**
     * @Column(type="string", length=50, nullable=true)
     * @var string
     */
    protected $institution;

    /**
     * @Column(type="text", length=65532, nullable=true)
     * @var text
     */
    protected $area_of_expertise;

    /**
     * @Column(type="string", length=100, nullable=true)
     * @var string
     */
    protected $cv_path;

    /**
     * @Column(type="string", length=50, nullable=true)
     * @var string
     */
    protected $facebook_link;

    /**
     * @Column(type="string", length=50, nullable=true)
     * @var string
     */
    protected $twitter_link;

    /**
     * @Column(type="string", length=50, nullable=true)
     * @var string
     */
    protected $instagram_link;

    /**
     * @Column(type="string", length=50, nullable=true)
     * @var string
     */
    protected $linkedin_link;

    /**
     * @Column(type="string", length=100)
     * @var string
     */
    protected $img_path;

    /**
     * @Column(type="string", length=50, nullable=true)
     * @var string
     */
    protected $university;

    /**
     * @Column(type="date", nullable=true)
     * @var date
     */
    protected $university_graduated_at;

    /**
     * @Column(type="string", length=50, nullable=true)
     * @var string
     */
    protected $md;

    /**
     * @Column(type="date", nullable=true)
     * @var date
     */
    protected $md_graduated_at;

    /**
     * @Column(type="string", length=50, nullable=true)
     * @var string
     */
    protected $associate_professor;

    /**
     * @Column(type="date", nullable=true)
     * @var date
     */
    protected $associate_professor_graduated_at;

    /**
     * @return mixed
     */
    public function getUsers() {
        return $this->users;
    }

    /**
     * @param mixed $users
     */
    public function setUsers($users) {
        $this->users = $users;
    }

    /**
     * @return string
     */
    public function getTitle() {
        return $this->title;
    }

    /**
     * @param string $title
     */
    public function setTitle($title) {
        $this->title = $title;
    }

    /**
     * @return string
     */
    public function getName() {
        return $this->name;
    }

    /**
     * @param string $name
     */
    public function setName($name) {
        $this->name = $name;
    }

    /**
     * @return string
     */
    public function getSurname() {
        return $this->surname;
    }

    /**
     * @param string $surname
     */
    public function setSurname($surname) {
        $this->surname = $surname;
    }

    /**
     * @return date
     */
    public function getBorn() {
        return $this->born;
    }

    /**
     * @param date $born
     */
    public function setBorn($born) {
        $this->born = $born;
    }

    /**
     * @return smallint
     */
    public function getGender() {
        return $this->gender;
    }

    /**
     * @param smallint $gender
     */
    public function setGender($gender) {
        $this->gender = $gender;
    }

    /**
     * @return text
     */
    public function getAddress() {
        return $this->address;
    }

    /**
     * @param text $address
     */
    public function setAddress($address) {
        $this->address = $address;
    }

    /**
     * @return int
     */
    public function getCountryId() {
        return $this->country_id;
    }

    /**
     * @param int $country_id
     */
    public function setCountryId($country_id) {
        $this->country_id = $country_id;
    }

    /**
     * @return text
     */
    public function getAbout() {
        return $this->about;
    }

    /**
     * @param text $about
     */
    public function setAbout($about) {
        $this->about = $about;
    }

    /**
     * @return string
     */
    public function getInstitution() {
        return $this->institution;
    }

    /**
     * @param string $institution
     */
    public function setInstitution($institution) {
        $this->institution = $institution;
    }

    /**
     * @return text
     */
    public function getAreaOfExpertise() {
        return $this->area_of_expertise;
    }

    /**
     * @param text $area_of_expertise
     */
    public function setAreaOfExpertise($area_of_expertise) {
        $this->area_of_expertise = $area_of_expertise;
    }

    /**
     * @return string
     */
    public function getCvPath() {
        return $this->cv_path;
    }

    /**
     * @param string $cv_path
     */
    public function setCvPath($cv_path) {
        $this->cv_path = $cv_path;
    }

    /**
     * @return string
     */
    public function getFacebookLink() {
        return $this->facebook_link;
    }

    /**
     * @param string $facebook_link
     */
    public function setFacebookLink($facebook_link) {
        $this->facebook_link = $facebook_link;
    }

    /**
     * @return string
     */
    public function getTwitterLink() {
        return $this->twitter_link;
    }

    /**
     * @param string $twitter_link
     */
    public function setTwitterLink($twitter_link) {
        $this->twitter_link = $twitter_link;
    }

    /**
     * @return string
     */
    public function getInstagramLink() {
        return $this->instagram_link;
    }

    /**
     * @param string $instagram_link
     */
    public function setInstagramLink($instagram_link) {
        $this->instagram_link = $instagram_link;
    }

    /**
     * @return string
     */
    public function getLinkedinLink() {
        return $this->linkedin_link;
    }

    /**
     * @param string $linkedin_link
     */
    public function setLinkedinLink($linkedin_link) {
        $this->linkedin_link = $linkedin_link;
    }

    /**
     * @return string
     */
    public function getImgPath() {
        return $this->img_path;
    }

    /**
     * @param string $img_path
     */
    public function setImgPath($img_path) {
        $this->img_path = $img_path;
    }

    /**
     * @return string
     */
    public function getUniversity() {
        return $this->university;
    }

    /**
     * @param string $university
     */
    public function setUniversity($university) {
        $this->university = $university;
    }

    /**
     * @return date
     */
    public function getUniversityGraduatedAt() {
        return $this->university_graduated_at;
    }

    /**
     * @param date $university_graduated_at
     */
    public function setUniversityGraduatedAt($university_graduated_at) {
        $this->university_graduated_at = $university_graduated_at;
    }

    /**
     * @return string
     */
    public function getMd() {
        return $this->md;
    }

    /**
     * @param string $md
     */
    public function setMd($md) {
        $this->md = $md;
    }

    /**
     * @return date
     */
    public function getMdGraduatedAt() {
        return $this->md_graduated_at;
    }

    /**
     * @param date $md_graduated_at
     */
    public function setMdGraduatedAt($md_graduated_at) {
        $this->md_graduated_at = $md_graduated_at;
    }

    /**
     * @return string
     */
    public function getAssociateProfessor() {
        return $this->associate_professor;
    }

    /**
     * @param string $associate_professor
     */
    public function setAssociateProfessor($associate_professor) {
        $this->associate_professor = $associate_professor;
    }

    /**
     * @return date
     */
    public function getAssociateProfessorGraduatedAt() {
        return $this->associate_professor_graduated_at;
    }

    /**
     * @param date $associate_professor_graduated_at
     */
    public function setAssociateProfessorGraduatedAt($associate_professor_graduated_at) {
        $this->associate_professor_graduated_at = $associate_professor_graduated_at;
    }
}

服务:

public function getUserByEmailAndPassword($email, $password, $ip, $user_agent) {
        $repository = $this->getEntityManager()->getRepository('App\Entity\Users');
        $user = $repository
            ->createQueryBuilder('u')
            ->addSelect('ui') // to limit queries when doing $user->getUserInformation()
            ->innerJoin('u.userInformation', 'ui')
            ->where('u.email = :email')->setParameter('email', $email)->getQuery()->getSingleResult();

        if ($user == null)
            return 0; // user not found

        $salt = $user->getSalt();
        $encrypted_password = $user->getEncryptedPassword();
        $hash = $this->checkhashSSHA($salt, $password);

        if ($hash == $encrypted_password) {
            return array(
                'id' => $user->getId(),
                'email' => $user->getEmail(),
                'type' => $user->getType(),
                'information' => $user->getUserInformation(),
                'created_at' => $user->getCreatedAt(),
                'updated_at' => $user->getUpdatedAt(),
                'is_deleted' => $user->getIsDeleted()
            );
        }
        return 1; // wrong email or password
    }

getUserInformation返回空... 如果我拨打getResultArray()而不是getSingleResult(),则信息会返回数据。但这一次,我不能这样使用$user->getId()。我只是返回$user。 所以,我想将$user称为对象数组。信息返回空?

  

{&#34;错误&#34;:false,&#34; user&#34;:{       &#34; id&#34;:27,       &#34;电子邮件&#34;:&#34; test@email.com",       &#34;输入&#34;:1,       &#34;信息&#34;:{},       &#34; created_at&#34;:{         &#34; date&#34;:&#34; 2017-01-01 21:05:45.000000&#34;,         &#34; timezone_type&#34;:3,         &#34;时区&#34;:&#34;欧洲/柏林&#34;       },       &#34; updated_at&#34;:{         &#34; date&#34;:&#34; 2017-01-01 21:05:45.000000&#34;,         &#34; timezone_type&#34;:3,         &#34;时区&#34;:&#34;欧洲/柏林&#34;       },       &#34; is_deleted&#34;:false}}

以下是getQuery()->getArrayResult();return $user;

  

{&#34;错误&#34;:false,&#34;用户&#34;:[       {         &#34;电子邮件&#34;:&#34; test@email.com",         &#34; encrypted_pa​​ssword&#34;:&#34; vjvyW2E9 / MT7pll01S6uBeFIvO9lNzI1N2VjYjEx&#34;,         &#34;盐&#34;:&#34; e7257ecb11&#34;,         &#34;输入&#34;:1,         &#34; sef_link&#34;:&#34; test-test&#34;,         &#34; id&#34;:27,         &#34; created_at&#34;:{           &#34; date&#34;:&#34; 2017-01-01 21:05:45.000000&#34;,           &#34; timezone_type&#34;:3,           &#34;时区&#34;:&#34;欧洲/柏林&#34;         },         &#34; updated_at&#34;:{           &#34; date&#34;:&#34; 2017-01-01 21:05:45.000000&#34;,           &#34; timezone_type&#34;:3,           &#34;时区&#34;:&#34;欧洲/柏林&#34;         },         &#34; is_deleted&#34;:false,         &#34; userInformation&#34;:{           &#34; title&#34;:&#34; test1&#34;,           &#34; name&#34;:&#34; test&#34;,           &#34;姓&#34;:&#34;测试&#34;,           &#34;出生&#34;:null,           &#34;性别&#34;:0,           &#34;地址&#34;:null,           &#34; country_id&#34;:null,           &#34;关于&#34;:null,           &#34;机构&#34;:null,           &#34; area_of_expertise&#34;:null,           &#34; cv_path&#34;:null,           &#34; facebook_link&#34;:null,           &#34; twitter_link&#34;:null,           &#34; instagram_link&#34;:null,           &#34; linkedin_link&#34;:null,           &#34; img_path&#34;:&#34; default-profile-female.png&#34;,           &#34;大学&#34;:null,           &#34; university_graduated_at&#34;:null,           &#34; md&#34;:null,           &#34; md_graduated_at&#34;:null,           &#34; associate_professor&#34;:null,           &#34; associate_professor_graduated_at&#34;:null,           &#34; id&#34;:16,           &#34; created_at&#34;:{             &#34; date&#34;:&#34; 2017-01-01 21:05:45.000000&#34;,             &#34; timezone_type&#34;:3,             &#34;时区&#34;:&#34;欧洲/柏林&#34;           },           &#34; updated_at&#34;:{             &#34; date&#34;:&#34; 2017-01-01 21:05:45.000000&#34;,             &#34; timezone_type&#34;:3,             &#34;时区&#34;:&#34;欧洲/柏林&#34;           },           &#34; is_deleted&#34;:false         }       }]}

1 个答案:

答案 0 :(得分:0)

我自己遇到过这个问题,当使用转储来检查要获取的数据并且它表示null时,您是否尝试过(例如)getUserInformation->getTitle()

只需进行for循环,foreach(users as getUserInformation)。应该这样做