2017-12-07 31 views
1

現在、私はsymfonyで2つのWebサイトを共同作業しています。メインWebサイトのデータベースを格納するバンドルを作成し、2番目のデータベースには独自のデータベースが格納されています。バンドル内のユーザー名または電子メールでユーザーを見つけることができません

私は、第2のウェブサイトの一部のユーザーをメインのユーザーに移行するコマンドを作成しようとしています。すべてのユーザーに対して、データベースに既に存在するかどうかを確認したいと思います。

私のデータベースがローカルの場合は、おそらく$email_exist = $userManager->findUserByEmail($email);のようなことをしますが、私は他のウェブサイトのuserManagerにアクセスしていません。 は、私が試した:

$emBug->getRepository('BugTrackerModelBundle:User')->findByEmail($email) 
$emBug->getRepository('BugTrackerModelBundle:User')->findBy(array('email' => $email)) 

が、私も同じことをしたと毎回私はエラーEntity 'BugTracker\ModelBundle\Entity\User' has no field 'email'. You can therefore not call 'findByEmail' on the entities' repositoryを取得し、私のリポジトリにカスタム関数を作成しました。 私は自分のデータベースに電子メールとユーザー名フィールドを持っていることは絶対に肯定的です(私は両方を試しました)。私は電子メールの代わりにidを試してみました。問題は実際には特にその分野にあるように働いていました。 私はFOSUserBundleを使用しています。ユーザークラスはBaseUser(ユーザー名と電子メールの送信元)です。

これを行う方法はありますか?新しいユーザープロバイダを追加する必要はありません。

私は「あなたはそうしてはいけない」と言っていないか、1つのデータベースしか持っていないと言ってください。それは有用ではなく、とにかく変更できません。

マイユーザエンティティ:

<?php 

namespace BugTracker\ModelBundle\Entity; 

use BugTracker\ModelBundle\Entity\Project\ProjectToUser; 
use BugTracker\ModelBundle\Entity\Authority\AuthorityToUser; 
use Doctrine\Common\Collections\ArrayCollection; 
use Doctrine\Common\Collections\Collection; 
use Doctrine\ORM\Mapping as ORM; 
use FOS\UserBundle\Model\User as BaseUser; 
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; 
use Symfony\Component\Validator\Constraints as Assert; 
use Symfony\Component\HttpFoundation\File\File as BaseFile; 
use Symfony\Component\HttpFoundation\File\UploadedFile; 
use Vich\UploaderBundle\Mapping\Annotation as Vich; 

/** 
* Class User 
* 
* @ORM\Table(name="user") 
* @UniqueEntity(fields={"email"}, message="A user with same email already exists") 
* @ORM\Entity(repositoryClass="BugTracker\ModelBundle\Repository\UserRepository") 
* @Vich\Uploadable 
*/ 
class User extends BaseUser 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @var bool 
    * @ORM\Column(type="boolean") 
    */ 
    protected $deleted; 

    /** 
    * @var bool 
    * @ORM\Column(name="notify", type="boolean", options={"default" : 1}, nullable=true) 
    */ 
    protected $notify; 

    /** 
    * @var string 
    * @ORM\Column(name="first_name", type="string", length=255, nullable=true) 
    */ 
    protected $firstName; 

    /** 
    * @var string 
    * @ORM\Column(name="last_name", type="string", length=255, nullable=true) 
    */ 
    protected $lastName; 

    /** 
    * @var string 
    * @ORM\Column(name="job_title", type="string", length=255, nullable=true) 
    */ 
    protected $jobTitle; 

    /** 
    * @var BaseFile|UploadedFile 
    * 
    * @Vich\UploadableField(mapping="user_image", fileNameProperty="image") 
    */ 
    protected $userImage; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="image", type="string", nullable=true) 
    */ 
    protected $image; 

    /** 
    * @var Company 
    * @ORM\ManyToOne(targetEntity="BugTracker\ModelBundle\Entity\Company", cascade={"persist"}) 
    * @ORM\JoinColumn(referencedColumnName="id") 
    */ 
    protected $company; 

    /** 
    * @var ArrayCollection 
    * @ORM\OneToMany(targetEntity="BugTracker\ModelBundle\Entity\Project", mappedBy="createdBy") 
    */ 
    protected $projects; 

    /** 
    * @var ArrayCollection 
    * @ORM\OneToMany(targetEntity="BugTracker\ModelBundle\Entity\Project\ProjectToUser", mappedBy="user", cascade={"all"}) 
    */ 
    protected $assignedProjects; 

    /** 
    * @var ArrayCollection 
    * @ORM\OneToMany(targetEntity="BugTracker\ModelBundle\Entity\Authority\AuthorityToUser", mappedBy="user", cascade={"all"}) 
    */ 
    protected $assignedAuthorities; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="created_at", type="datetime", nullable=true) 
    */ 
    protected $createdAt; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="updated_at", type="datetime", nullable=true) 
    */ 
    protected $updatedAt; 

    /** 
    * @var Country 
    * @ORM\ManyToOne(targetEntity="BugTracker\ModelBundle\Entity\Country", cascade={"persist"}) 
    * @ORM\JoinColumn(referencedColumnName="id") 
    */ 
    protected $country; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="time_zone", type="string", nullable=true) 
    */ 
    protected $timeZone; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="user_role", type="string", nullable=false) 
    */ 
    protected $userRole; 

    /** 
    * User constructor. 
    */ 
    public function __construct() 
    { 
     parent::__construct(); 

     $this->enabled = true; 
     $this->deleted = false; 
     $this->notify = true; 

     $this->createdAt = new \DateTime(); 
     $this->updatedAt = new \DateTime(); 

     $this->projects = new ArrayCollection(); 
     $this->assignedProjects = new ArrayCollection(); 
     $this->assignedAuthorities = new ArrayCollection(); 
    } 

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

    /** 
    * Set deleted 
    * 
    * @param bool $deleted 
    * @return $this 
    */ 
    public function setDeleted($deleted) 
    { 
     $this->deleted = $deleted; 

     return $this; 
    } 

    /** 
    * Get deleted 
    * 
    * @return bool 
    */ 
    public function isDeleted() 
    { 
     return $this->deleted; 
    } 

    /** 
    * Set notify 
    * 
    * @param bool $notify 
    * @return $this 
    */ 
    public function setNotify($notify) 
    { 
     $this->notify = $notify; 

     return $this; 
    } 

    /** 
    * Get notify 
    * 
    * @return bool 
    */ 
    public function getNotify() 
    { 
     return $this->notify; 
    } 

    /** 
    * Set firstName 
    * 
    * @param string $firstName 
    * @return $this 
    */ 
    public function setFirstName($firstName) 
    { 
     $this->firstName = $firstName; 

     return $this; 
    } 

    /** 
    * Get firstName 
    * 
    * @return string 
    */ 
    public function getFirstName() 
    { 
     return $this->firstName; 
    } 

    /** 
    * Set lastName 
    * 
    * @param string $lastName 
    * @return $this 
    */ 
    public function setLastName($lastName) 
    { 
     $this->lastName = $lastName; 

     return $this; 
    } 

    /** 
    * Get lastName 
    * 
    * @return string 
    */ 
    public function getLastName() 
    { 
     return $this->lastName; 
    } 

    /** 
    * Get full name 
    * 
    * @return string 
    */ 
    public function getFullName() 
    { 
     return trim($this->firstName.' '.$this->lastName); 
    } 

    /** 
    * Set jobTitle 
    * 
    * @param string $jobTitle 
    * @return $this 
    */ 
    public function setJobTitle($jobTitle) 
    { 
     $this->jobTitle = $jobTitle; 

     return $this; 
    } 

    /** 
    * Get jobTitle 
    * 
    * @return string 
    */ 
    public function getJobTitle() 
    { 
     return $this->jobTitle; 
    } 

    /** 
    * Set user image source 
    * 
    * @param UploadedFile|BaseFile $userImage 
    * @return $this 
    */ 
    public function setUserImage($userImage = null) 
    { 
     $this->userImage = $userImage; 

     if ($userImage) { 
      // It is required that at least one field changes if you are using doctrine 
      // otherwise the event listeners won't be called and the file is lost 
      $this->updatedAt = new \DateTime(); 
     } 

     return $this; 
    } 

    /** 
    * Get user image source 
    * 
    * @return BaseFile|UploadedFile 
    */ 
    public function getUserImage() 
    { 
     return $this->userImage; 
    } 

    /** 
    * Set image 
    * 
    * @param string $image 
    * @return $this 
    */ 
    public function setImage($image) 
    { 
     $this->image = $image; 

     return $this; 
    } 

    /** 
    * Get image 
    * 
    * @return string 
    */ 
    public function getImage() 
    { 
     return $this->image; 
    } 

    /** 
    * Set company 
    * 
    * @param Company $company 
    * @return $this 
    */ 
    public function setCompany(Company $company = null) 
    { 
     $this->company = $company; 

     return $this; 
    } 

    /** 
    * Get company 
    * 
    * @return Company 
    */ 
    public function getCompany() 
    { 
     return $this->company; 
    } 

    /** 
    * Set email 
    * 
    * @param string $email 
    * @return $this 
    */ 
    public function setEmail($email) 
    { 
     parent::setEmail($email); 
     parent::setUsername($email); 

     return $this; 
    } 

    /** 
    * @return array 
    */ 
    public function getUserRoles() 
    { 
     return $this->roles; 
    } 

    public function setUserRole($role) 
    { 
     $this->userRole = $role; 
     $this->roles = array($role); 
    } 

    public function getUserRole() 
    { 
     return $this->userRole; 
    } 

    public function getRoles() 
    { 
     return $this->roles; 
    } 


    /** 
    * Add project 
    * 
    * @param Project $project 
    * @return $this 
    */ 
    public function addProject(Project $project) 
    { 
     $this->projects->add($project); 

     return $this; 
    } 

    /** 
    * Get projects 
    * 
    * @return Collection 
    */ 
    public function getProjects() 
    { 
     return $this->projects; 
    } 

    /** 
    * Remove project 
    * 
    * @param Project $project 
    */ 
    public function removeProject(Project $project) 
    { 
     $this->projects->removeElement($project); 
    } 

    /** 
    * Add assignedProject 
    * 
    * @param ProjectToUser $assignedProject 
    * @return $this 
    */ 
    public function addAssignedProject(ProjectToUser $assignedProject) 
    { 
     $this->assignedProjects->add($assignedProject); 

     return $this; 
    } 

    /** 
    * Get assignedProjects 
    * 
    * @return Collection 
    */ 
    public function getAssignedProjects() 
    { 
     return $this->assignedProjects; 
    } 

    /** 
    * Get assignedAuthorities 
    * 
    * @return Collection 
    */ 
    public function getAssignedAuthorities() 
    { 
     return $this->assignedAuthorities; 
    } 

    /** 
    * Remove assignedProject 
    * 
    * @param ProjectToUser $assignedProject 
    */ 
    public function removeAssignedProject(ProjectToUser $assignedProject) 
    { 
     $this->assignedProjects->removeElement($assignedProject); 
    } 

    /** 
    * Set createdAt 
    * 
    * @param \DateTime $createdAt 
    * @return $this 
    */ 
    public function setCreatedAt($createdAt) 
    { 
     $this->createdAt = $createdAt; 

     return $this; 
    } 

    /** 
    * Get createdAt 
    * 
    * @return \DateTime 
    */ 
    public function getCreatedAt() 
    { 
     return $this->createdAt; 
    } 

    /** 
    * Set updatedAt 
    * 
    * @param \DateTime $updatedAt 
    * @return $this 
    */ 
    public function setUpdatedAt($updatedAt) 
    { 
     $this->updatedAt = $updatedAt; 

     return $this; 
    } 

    /** 
    * Get updatedAt 
    * 
    * @return \DateTime 
    */ 
    public function getUpdatedAt() 
    { 
     return $this->updatedAt; 
    } 

    /** 
    * @return Country 
    */ 
    public function getCountry() 
    { 
     return $this->country; 
    } 

    /** 
    * @param Country $country 
    */ 
    public function setCountry($country) 
    { 
     $this->country = $country; 
    } 

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

    /** 
    * @param string $timeZone 
    */ 
    public function setTimeZone($timeZone) 
    { 
     $this->timeZone = $timeZone; 
     return $this; 
    } 


    /** 
    * Get authority 
    * 
    * @return Authority 
    */ 
    public function getAuthority() 
    { 
     return $this->getCompany()->getAuthority(); 
    } 

    /** 
    * Get authorities 
    * 
    * @return array 
    */ 
    public function getAuthorities() 
    { 
     $authorities = array($this->getAuthority()); 
     foreach ($this->getAssignedAuthorities() as $authority) { 
      $authorities[] = $authority->getAuthority(); 
     } 
     return $authorities; 
    } 

    /** 
    * Add assignedAuthorities 
    * 
    * @param BugTracker\ModelBundle\Entity\Authority\AuthorityToUser $assignedAuthority 
    * @return $this 
    */ 
    public function addAssignedAuthority($assignedAuthority) 
    { 
     $this->assignedAuthorities->add($assignedAuthority); 

     return $this; 
    } 

    public function isAccountNonLocked() 
    { 
     if($this->getCompany()) 
      return $this->getAuthority()->isEnabled(); 
     return false; 
    } 
} 
+0

BugTrackerModelBundle:ユーザーエンティティを共有してください。 –

+0

@StephanVierkant私はそれを加えました。 –

答えて

1

何あなたの投稿エンティティの電子メールプロパティに応じて、親クラスFOS\UserBundle\Model\Userに存在し、あなたの子供のクラスは、この機能も、それにsetEmail(){....}のような電子メールを設定しますので、あなたが、代わりに電子メールのユーザー名を使用する場合parent::setUsername($email);をユーザ名に同じ値が設定されますので、私はあなたがusername財産

$emBug->getRepository('BugTrackerModelBundle:User')->findBy(array('username' => $email)); 

それともelesユーザーが定義したように、あなたのクラスでのメールプロパティを定義を使用してユーザーを取得することができますねdユーザー名プロパティ

/** 
* @var string 
* @ORM\Column(type="string" definition....) 
*/ 
protected $email; 
+0

私は電子メールで試したものすべて私はユーザー名でも試しました...エンティティに追加したユーザー名はテストでしたが、私のコマンドは機能しましたが、ウェブサイト(フィールドduplicata)を開こうとしたときに例外を与えましたそれを削除して、別の解決策を見つけてください。私は質問からそれを削除します。 –