2
votes

I'm using FOSUserBundle for my users, Alice for fixture generation, and FOSRestBundle. I have 2 entities: User and Shift. A user can have many shifts. Below are my entity classes, and this is the error I get when I try to login as a valid user:

[2015-08-11 18:50:47] request.CRITICAL: Uncaught PHP Exception Symfony\Component\Debug\Exception\ContextErrorException: "Notice: Undefined index: user" at /var/www/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1758 {"exception":"[object]

EDIT: line 1758 of BasicEntityPersister is looking at the mappedBy value. Still not sure what is wrong with it though.

EDIT: Also found this related topic Doctrine: Value of mappedBy ignored in OneToMany Association

If I remove the fetch="EAGER" values I can login, otherwise I get the above error. I've scoured over this for almost 3 hours now and can't make heads or tails of it.

My tables if it helps:

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `username_canonical` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `email_canonical` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `enabled` tinyint(1) NOT NULL,
  `salt` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `last_login` datetime DEFAULT NULL,
  `locked` tinyint(1) NOT NULL,
  `expired` tinyint(1) NOT NULL,
  `expires_at` datetime DEFAULT NULL,
  `confirmation_token` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `password_requested_at` datetime DEFAULT NULL,
  `roles` longtext COLLATE utf8_unicode_ci NOT NULL COMMENT '(DC2Type:array)',
  `credentials_expired` tinyint(1) NOT NULL,
  `credentials_expire_at` datetime DEFAULT NULL,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `phone` varchar(16) COLLATE utf8_unicode_ci NOT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `UNIQ_8D93D64992FC23A8` (`username_canonical`),
  UNIQUE KEY `UNIQ_8D93D649A0D96FBF` (`email_canonical`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `shift` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user` int(11) NOT NULL,
  `break` double NOT NULL,
  `start_time` datetime NOT NULL,
  `end_time` datetime NOT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=51 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

User Class:

<?php

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Gedmo\Mapping\Annotation as Gedmo;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="AppBundle\Data\Repository\UserRepository")
 * @ExclusionPolicy("all")
 */
class User extends BaseUser
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;


    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     * @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
     * @Assert\Length(
     *     min=3,
     *     max=255,
     *     minMessage="The name is too short.",
     *     maxMessage="The name is too long.",
     *     groups={"Registration", "Profile"}
     * )
     * @Expose
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="phone", type="string", length=16)
     * @Expose
     */
    private $phone;

    /**
     * @var \DateTime
     *
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(name="created_at", type="datetime")
     * @Expose
     */
    private $createdAt;

    /**
     * @var \DateTime
     *
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(name="updated_at", type="datetime")
     * @Expose
     */
    private $updatedAt;

    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="Shift", mappedBy="user", fetch="EAGER")
     * @Expose
     */
    private $shifts;


    public function __construct()
    {
        $this->shifts = new ArrayCollection();
        parent::__construct();
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set phone
     *
     * @param string $phone
     * @return User
     */
    public function setPhone($phone)
    {
        $this->phone = $phone;

        return $this;
    }

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

    /**
     * Set createdAt
     *
     * @param \DateTime $createdAt
     * @return User
     */
    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 User
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

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

    /**
     * Add shifts
     *
     * @param \AppBundle\Entity\Shift $shifts
     * @return User
     */
    public function addShift(\AppBundle\Entity\Shift $shifts)
    {
        $this->shifts[] = $shifts;

        return $this;
    }

    /**
     * Remove shifts
     *
     * @param \AppBundle\Entity\Shift $shifts
     */
    public function removeShift(\AppBundle\Entity\Shift $shifts)
    {
        $this->shifts->removeElement($shifts);
    }

    /**
     * Get shifts
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getShifts()
    {
        return $this->shifts;
    }
}

Shift Class:

<?php

namespace AppBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;

/**
 * Shift
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="AppBundle\Data\Repository\ShiftRepository")
 * @ExclusionPolicy("all")
 */
class Shift
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\Column(name="user", type="integer")
     * @ORM\ManyToOne(targetEntity="User", inversedBy="shifts", fetch="EAGER")
     * @Expose
     */
    private $user;

    /**
     * @var float
     *
     * @ORM\Column(name="break", type="float")
     * @Expose
     */
    private $break;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="start_time", type="datetime")
     * @Expose
     */
    private $startTime;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="end_time", type="datetime")
     * @Expose
     */
    private $endTime;

    /**
     * @var \DateTime
     *
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(name="created_at", type="datetime")
     * @Expose
     */
    private $createdAt;

    /**
     * @var \DateTime
     *
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(name="updated_at", type="datetime")
     * @Expose
     */
    private $updatedAt;


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

    /**
     * Set user
     *
     * @param User $user
     * @return Shift
     */
    public function setUser($user)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return User
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * Set break
     *
     * @param float $break
     * @return Shift
     */
    public function setBreak($break)
    {
        $this->break = $break;

        return $this;
    }

    /**
     * Get break
     *
     * @return float 
     */
    public function getBreak()
    {
        return $this->break;
    }

    /**
     * Set startTime
     *
     * @param \DateTime $startTime
     * @return Shift
     */
    public function setStartTime($startTime)
    {
        $this->startTime = $startTime;

        return $this;
    }

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

    /**
     * Set endTime
     *
     * @param \DateTime $endTime
     * @return Shift
     */
    public function setEndTime($endTime)
    {
        $this->endTime = $endTime;

        return $this;
    }

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

    /**
     * Set createdAt
     *
     * @param \DateTime $createdAt
     * @return Shift
     */
    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 Shift
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

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

Join query I need to perform that isn't working, and is throwing the following error:

$shiftsQuery = $this->createQueryBuilder('s1')
            ->addSelect('u')
            ->from('AppBundle:Shift', 's2')
            ->leftJoin('s1.user', 'u')
            ->where('u = :user')
            ->andWhere('s1.user = :user AND s2.user != :user')
            ->andWhere('s2.startTime BETWEEN s1.startTime AND s1.endTime OR s2.endTime BETWEEN s1.startTime AND s1.endTime')
            ->setParameter('user', $user)
            ->groupBy('s1.id')
        ;

        $result = $shiftsQuery
            ->getQuery()
            ->setFetchMode('AppBundle\Entity\User', 'shifts', \Doctrine\ORM\Mapping\ClassMetadata::FETCH_EAGER)
            ->getResult();

Error:

Doctrine\ORM\Query\QueryException::semanticalError('line 0, col 62 near 'u, AppBundle:Shift': Error: Class AppBundle\Entity\Shift has no association named user', object(QueryException)) in /var/www/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php line 483

2
Fixed the login issue by removing fetch="EAGER" from the User entity, but something still isn't right. When working on a join query in my repository I get the following error regardless of what my leftJoin statement looks like: QueryException::semanticalError('line 0, col 62 near 'u, AppBundle:Shift': Error: Class AppBundle\Entity\Shift has no association named userBrian Litzinger

2 Answers

1
votes

In Shift.php entity, $user property has not correct annotation, try this:

/**
 * @var User
 * @ORM\ManyToOne(targetEntity="User", inversedBy="shifts", fetch="EAGER")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 * @Expose
 **/
private $user;

and then either make php doctine:schema:update --force or doctine:migrations:diff and then migrate up.

0
votes

Dan Mironis' annotation comment above was part of the problem. The other part, was how I was defining my fixtures. After making this update the query in my original post works as expected. I was just randomly assigning a user id on the shifts, so I updated it from:

AppBundle\Entity\Shift:
    shift{1..50}:
        userId: <numberBetween(1, 2)>
        break: <numberBetween(1, 8)>
        startTime: <dateTimeBetween('8AM', '12PM')>
        endTime: <dateTimeBetween($startTime, '+8 hours')>

to:

AppBundle\Entity\Shift: shift{1..20}: user: @user2 break: startTime: endTime:

shift{21..40}:
    user: @user3
    break: <numberBetween(1, 8)>
    startTime: <dateTimeBetween('now', '+3 days')>
    endTime: <dateTimeBetween('now', '+3 days')>

shift{41..60}:
    user: @user4
    break: <numberBetween(1, 8)>
    startTime: <dateTimeBetween('now', '+3 days')>
    endTime: <dateTimeBetween('now', '+3 days')>

shift{61..80}:
    user: @user5
    break: <numberBetween(1, 8)>
    startTime: <dateTimeBetween('now', '+3 days')>
    endTime: <dateTimeBetween('now', '+3 days')>