ManyToOne relation Symfony2 - php

I have a many to one relation between my entities.
An application can have activities
<?php
namespace AppAcademic\ApplicationBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Activity
*
* #ORM\Table()
* #ORM\Entity
*/
class Activity
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="AppAcademic\ApplicationBundle\Entity\Application", inversedBy="activity")
* #ORM\JoinColumn(name="application_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $application;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
public function setApplication($application)
{
$this->application = $application;
return $this;
}
public function getApplication()
{
return $this->application;
}
/**
* Set title
*
* #param string $title
* #return Activity
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
}
And the application
/**
* Application
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="AppAcademic\ApplicationBundle\Entity\ApplicationRepository")
*/
class Application
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\OneToMany(targetEntity="AppAcademic\ApplicationBundle\Entity\Activity", mappedBy="application")
*/
protected $activities;
...
}
I have this in the profiler:
AppAcademic\ApplicationBundle\Entity\Application
The mappings AppAcademic\ApplicationBundle\Entity\Application#activities and AppAcademic\ApplicationBundle\Entity\Activity#application are inconsistent with each other.
AppAcademic\ApplicationBundle\Entity\Application
The mappings AppAcademic\ApplicationBundle\Entity\Application#activities and AppAcademic\ApplicationBundle\Entity\Activity#application are inconsistent with each other.
AppAcademic\ApplicationBundle\Entity\Activity
The association AppAcademic\ApplicationBundle\Entity\Activity#application refers to the inverse side field AppAcademic\ApplicationBundle\Entity\Application#activity which does not exist.

For the mapping annotation ManyToOne on the Activity::$application property, the attribute
inversedBy="activity"
This should be
inversedBy="activities"

Related

Symfony 3 / Doctrine many-to-one always returns just last addition- FOSUser/OAuth extended

I am working on a login page, where it should be possible to login with different socialnetwork logins (facebook, twitter, google, etc).
It should work like this: every user has the possibility to add the socialnetwork login that he/she wants, so I want to have it as an One User-> Many Socialnetworklogins relation.
What works: Data is stored in Database, but in the line
var_dump($user->getSocialnetworks());
I just get one Object, not all the ones that should be related to this user. I know that the function justATest adds just a socialnetworklogin to user 1, this was just a test.
<?php
namespace AppBundle\Entity\Registration;
use Doctrine\Common\Collections\ArrayCollection;
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider as BaseClass;
use Symfony\Component\Security\Core\User\UserInterface;
class FOSUBUserProvider extends BaseClass
{
public function justATest(UserResponseInterface $response)
{
// get user_id and socialnetworkname from response
$userIdInSocialNetwork = $response->getUsername();
$socialnetwork = $response->getResourceOwner()->getName();
$login= new UserInSocialNetworks();
$login->setSocialIdentyfier($response->getEmail());
$login->setSocialNetworkSlug($socialnetwork);
$user = $this->userManager->findUserBy(array(
'id' => 1)
);
$login->setUserId($user);
$user->addSocialNetwork($login);
$this->userManager->updateUser($user);
$users= $this->userManager->findUsers();
var_dump($users);
var_dump($socialnetwork);
var_dump($user->getSocialnetworks());
die();
} }
The User Class:
<?php
/*
* This is the User class, depending on fos_userBundle
*/
namespace AppBundle\Entity\Registration;
use Doctrine\Common\Collections\ArrayCollection as ArrayCollection;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\OneToMany(targetEntity="UserInSocialNetworks", mappedBy="user_id", cascade={"persist", "remove"})
* #var ArrayCollection|null
*/
private $socialnetworks;
/**
* #var string
*
* #ORM\Column(name="family_name",type="string", length=255, nullable=true)
*/
private $familyName;
/**
* #var string
*
* #ORM\Column(name="given_name",type="string", length=255, nullable=true)
*/
private $givenName;
public function __construct()
{
parent::__construct();
$this->socialnetworks = new ArrayCollection();
}
/**
* #return string
*/
public function getFamilyName()
{
return $this->familyName;
}
/**
* #param string $familyName
*/
public function setFamilyName($familyName)
{
$this->familyName = $familyName;
}
/**
* #return string
*/
public function getGivenName()
{
return $this->givenName;
}
/**
* #param string $givenName
*/
public function setGivenName($givenName)
{
$this->givenName = $givenName;
}
/**
* #return ArrayCollection
*/
public function getSocialnetworks()
{
return $this->socialnetworks;
}
/**
* #param Collection UserInSocialNetworks
*/
public function setSocialnetworks($socialnetworks)
{
$this->socialnetworks = $socialnetworks;
}
/**
* #param UserInSocialNetwork
*/
public function addSocialNetwork($socialnetwork)
{
$this->getSocialnetworks()->add($socialnetwork);
}
}
The Class where Socialnetworks for Users are stored:
<?php
namespace AppBundle\Entity\Registration;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* UserInSocialNetworks
*
* #ORM\Table(name="user_in_social_networks")
* #ORM\Entity(repositoryClass="AppBundle\Repository\Registration\UserInSocialNetworksRepository")
*/
class UserInSocialNetworks
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="User", inversedBy="socialnetworks")
* #ORM\JoinColumn(name="user_id")
* #var User
*
*/
private $userId;
/**
* #var int
*
* #ORM\Column(name="social_network_slug", type="string", length=255, nullable=true)
*/
private $socialNetworkSlug;
/**
* #var string
*
* #ORM\Column(name="social_identifier", type="string", length=255, nullable=true)
*/
private $socialIdentyfier;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set userId
*
* #param integer $userId
*
* #return UserInSocialNetworks
*/
public function setUserId($userId)
{
$this->userId = $userId;
return $this;
}
/**
* Get userId
*
* #return int
*/
public function getUserId()
{
return $this->userId;
}
/**
* #return int
*/
public function getSocialNetworkSlug()
{
return $this->socialNetworkSlug;
}
/**
* #param int $socialNetworkSlug
*/
public function setSocialNetworkSlug($socialNetworkSlug)
{
$this->socialNetworkSlug = $socialNetworkSlug;
}
/**
* #return string
*/
public function getSocialIdentyfier()
{
return $this->socialIdentyfier;
}
/**
* #param string $socialIdentyfier
*/
public function setSocialIdentyfier($socialIdentyfier)
{
$this->socialIdentyfier = $socialIdentyfier;
}
}
This is what I get
object(Doctrine\ORM\PersistentCollection)[545]
private 'snapshot' =>
array (size=1) <------- just one
0 =>
object(AppBundle\Entity\Registration\UserInSocialNetworks)[336]
private 'id' => int 5 <---- I added already 5 line with my code to database
private 'userId' =>
object(AppBundle\Entity\Registration\User)[516]
...
private 'socialNetworkSlug' => string 'google' (length=6)
private 'socialIdentyfier' => string 'blablabla#googlemail.com' (length=24)
sorry, my fault,
the solution was to correct the relations:
class UserInSocialNetworks
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* Many Socialnetwork Logins have one User
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Registration\User", inversedBy="socialnetworks")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*
*/
private $user;
and
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* One User can have many social networks
* #ORM\OneToMany(targetEntity="UserInSocialNetworks", mappedBy="user", cascade={"remove"})
*/
private $socialnetworks;

ongr-io elasticsearch bundle implementation with doctrine symfony2

In our symfony2 project we want to implement elasticsearch with ongr-io bundle, but our whole system is built on doctrine. Is it possible to somehow use ongr-io elasticsearch bundle without totally redoing whole database with documents instead of entities?
Entity that I want to index (fields for search would be: name, ChildCategory and ParentCategory):
/**
* Course
*
* #ORM\Table(name="course")
* #ORM\Entity(repositoryClass="CoreBundle\Repository\CourseRepository")
* #ORM\HasLifecycleCallbacks
*/
class Course
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, unique=false)
*/
private $name;
/**
* #var ParentCategory
*
* #ORM\ManyToOne(targetEntity="ParentCategory")
* #ORM\JoinColumn(name="parentCategory_id", referencedColumnName="id")
*/
private $parentCategory;
/**
* #var ChildCategory
*
* #ORM\ManyToOne(targetEntity="ChildCategory", inversedBy="courses")
* #ORM\JoinColumn(name="childCategory_id", referencedColumnName="id")
*/
private $childCategory;
/**
* #var City
*
* #ORM\ManyToOne(targetEntity="City")
* #ORM\JoinColumn(name="city_id", referencedColumnName="id")
*/
private $city;
/**
* #var Users
*
* #ORM\ManyToOne(targetEntity="UserBundle\Entity\Users", inversedBy="course")
* #ORM\JoinColumn(name="owner_id", referencedColumnName="id")
*/
private $owner;
/**
* #var text
*
* #ORM\Column(name="description", type="text")
*/
private $description;
/**
* #var Picture
*
* #ORM\OneToMany(targetEntity="CoreBundle\Entity\Picture", mappedBy="course", cascade={"persist", "remove"})
*/
private $pictures;
/**
* #var Ratings
*
* #ORM\OneToMany(targetEntity="CoreBundle\Entity\Ratings", mappedBy="courseid")
*/
private $ratings;
public $avgRating;
}
it's very interesting.
The first thing is Entity location. The document for ElasticsearchBundle has to be in Document directory. It's not a big deal. All you need to do is to create an empty class and extend entity with #Document annotation. Next are the fields. With name field there is no problem at all, just add #property annotation and you are good. More interesting is with relations. My suggestion would be to create separate property and in the getter return value from the relation for that field. I hope you got the idea.
Update:
Here's an example of documents:
AppBundle/Entity/Post
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use ONGR\ElasticsearchBundle\Annotation as ES;
/**
* Post
*
* #ORM\Table(name="post")
* #ORM\Entity(repositoryClass="AppBundle\Repository\PostRepository")
*/
class Post
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ES\Property(type="string")
* #ORM\Column(name="title", type="string", length=255, nullable=true)
*/
private $title;
/**
* #var User
*
* #ORM\ManyToOne(targetEntity="User")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
/**
* #var string
*
* #ES\Property(name="user", type="string")
*/
private $esUser;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
*
* #return Post
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set user
*
* #param \AppBundle\Entity\User $user
*
* #return Post
*/
public function setUser(\AppBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \AppBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* #return string
*/
public function getEsUser()
{
return $this->esUser;
}
/**
* #param string $esUser
*/
public function setEsUser($esUser)
{
$this->esUser = $esUser;
}
}
AppBundle/Document/Post
<?php
namespace AppBundle\Document;
use ONGR\ElasticsearchBundle\Annotation as ES;
use AppBundle\Entity\Post as OldPost;
/**
* #ES\Document()
*/
class Post extends OldPost
{
}

Many-To-Many relationship Doctrine

I have had quite a few problems with my current entities mapping, so I wanted an opinion on what is the issue with it.
Let's say with have the following tables example with Doctrine, but also keep in mind that a Group can have many Users, and a User can be part of many Groups
User
--
id
name
Group
--
id
title
User Groups
--
id
user_id
group_id
And this is my current approach:
/**
* User
*
* #ORM\Table(name="user")
* #ORM\Entity(repositoryClass="SomeBundle\Entity\Repository\UserRepository")
*/
class User
{
/**
* #var integer
* #ORM\Id
* #ORM\Column(name="user_id", type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $user_id;
// some other fields and functions here
}
Group
/**
* Group
*
* #ORM\Table(name="group", indexes={#ORM\Index(name="group_parent", columns={"parent_id"})})
* #ORM\Entity(repositoryClass="SomeBundle\Entity\Repository\GroupRepository")
*/
class Group
{
/**
* #var integer
*
* #ORM\Column(name="category_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $categoryId;
// some other functions and variables here
}
...and user_groups
/**
* User_Groups
*
* #ORM\Table(name="user_groups",
* indexes={
* #ORM\Index(name="user_group_user", columns={"user_id"}),
* #ORM\Index(name="user_group_group", columns={"group_id"})
* }
* )
* #ORM\Entity(repositoryClass="SomeBundle\Entity\Repository\UserGroupRepository")
*/
class UserGroup
{
/**
* #var integer
*
* #ORM\Id
* #ORM\Column(name="user_group_id", type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $userGroupId;
/**
* #var \SomeBundle\Entity\User
* #ORM\Column(name="user_id")
*
* #ORM\ManyToMany(targetEntity="SomeBundle\Entity\User", inversedBy="userGroups")
* #ORM\JoinTable(name="users",
* joinColumns={#ORM\JoinColumn(name="user_id", referencedColumnName="userId")})
*/
private $user;
/**
* #var SomeBundle\Entity\Category
*
* #ORM\ManyToOne(targetEntity="SomeBundle\Entity\Group", inversedBy="groups")
* #ORM\JoinColumn(name="group_id", referencedColumnName="group_id")
*
*/
private $group;
/// etc
}
Any help would be appreciated!
/** #Entity **/
class User
{
// ...
/**
* #ManyToMany(targetEntity="Group", inversedBy="users")
* #JoinTable(name="users_groups")
**/
private $groups;
public function __construct() {
$this->groups = new \Doctrine\Common\Collections\ArrayCollection();
}
// ...
}
/** #Entity **/
class Group
{
// ...
/**
* #ManyToMany(targetEntity="User", mappedBy="groups")
**/
private $users;
public function __construct() {
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
}
// ...
}
Your case is Many-to-Many, Bidirectional Association
Doctrine association mapping
In this example, I have a product and a mesureUnit they relate many to many.
In your case you only need user and group. The UserGroups is generated automatically.
Many to many relationship are done as follows:
<?php
namespace TeamERP\StoresBundle\Entity;
use Doctrine\ORM\Mapping AS ORM;
/**
* #ORM\Entity
*/
class MesureUnit
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=100, nullable=true)
*/
private $name;
/**
* #ORM\Column(type="string", length=10, nullable=true)
*/
private $abreviation;
/**
* #ORM\OneToMany(targetEntity="TeamERP\StoresBundle\Entity\ProductActivity", mappedBy="mesureUnit")
*/
private $pproductActivity;
/**
* #ORM\ManyToMany(targetEntity="TeamERP\StoresBundle\Entity\Product", mappedBy="mesureUnit")
*/
private $product;
/**
* Constructor
*/
public function __construct()
{
$this->pproductActivity = new \Doctrine\Common\Collections\ArrayCollection();
$this->product = new \Doctrine\Common\Collections\ArrayCollection();
}
Then the produc class could be like:
<?php
namespace TeamERP\StoresBundle\Entity;
use Doctrine\ORM\Mapping AS ORM;
/**
* #ORM\Entity
*/
class Product
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=50, nullable=true)
*/
private $code;
/**
* #ORM\Column(type="string", length=250, nullable=true)
*/
private $description;
/**
* #ORM\Column(type="datetime", nullable=true)
*/
private $date_received;
/**
* #ORM\OneToMany(targetEntity="TeamERP\StoresBundle\Entity\ProductActivity", mappedBy="product")
*/
private $pproductActivity;
/**
* #ORM\ManyToOne(targetEntity="TeamERP\StoresBundle\Entity\Category", inversedBy="product")
* #ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
private $category;
/**
* #ORM\ManyToOne(targetEntity="TeamERP\StoresBundle\Entity\AJob", inversedBy="product")
* #ORM\JoinColumn(name="job_id", referencedColumnName="id")
*/
private $aJob;
/**
* #ORM\ManyToMany(targetEntity="TeamERP\StoresBundle\Entity\MesureUnit", inversedBy="product")
* #ORM\JoinTable(
* name="MesureUnit2Product",
* joinColumns={#ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=false)},
* inverseJoinColumns={#ORM\JoinColumn(name="mesure_unit_id", referencedColumnName="id", nullable=false)}
* )
*/
private $mesureUnit;
/**
* Constructor
*/
public function __construct()
{
$this->pproductActivity = new \Doctrine\Common\Collections\ArrayCollection();
$this->mesureUnit = new \Doctrine\Common\Collections\ArrayCollection();
}
Yo do not need to create the table manually. It is generated by Doctrine.
Hope it helps

Zend Framework 2 Doctrine2 leftJoin creating a separate SELECT query

Does anyone know why the Doctrine query below creates a separate join for the StandardDedescribed entity?
As you can see below this query pulls the information from the master entity 'EquipmentUK' and two related tables: 'SchemaInfo' and 'StandardDescribed'. It works well for the SchemaInfo, but creates another query for StandardDescribed. The relations in the Entities seem to be coded extactly the same, so I don't understand why it behaves differently for StandardDescribed?
$qb->select('e')
->from('\Lookup\Entity\Fullhistory\EquipmentUK', 'e')
->leftJoin(
'\Lookup\Entity\Fullhistory\SchemaInfo',
'si',
Expr\Join::WITH,
'e.schema_id = si.schema_id AND si.parent_schema_id != si.schema_id'
)
->leftJoin(
'\Lookup\Entity\Intelligence\StandardDescribedUK',
'sd',
Expr\Join::WITH,
'e.schema_id = sd.schema_id'
)
->where($qb->expr()->eq('e.vehicle_id', '?1'))
->setParameter(1, $jatoid);
Here are the Entities:
EquipmentUK:
namespace Lookup\Entity\Fullhistory;
use Doctrine\ORM\Mapping as ORM;
/**
* EquipmentUK
*
* #ORM\Table(name="equipment",
* uniqueConstraints={
* #ORM\UniqueConstraint(name="search_idx", columns={"vehicle_id", "option_id", "record_id", "schema_id"})
* }
* )
* #ORM\Entity
*/
class EquipmentUK
{
/**
* #var integer
*
* #ORM\Id
* #ORM\Column(type="integer")
*/
private $vehicle_id;
/**
* #var integer
*
* #ORM\Id
* #ORM\Column(type="integer")
*/
private $schema_id;
/**
* #var integer
*
* #ORM\Id
* #ORM\Column(type="integer")
*/
private $option_id;
/**
* #var integer
*
* #ORM\Id
* #ORM\Column(type="integer")
*/
private $record_id;
/**
* #var string
*
* #ORM\Column(type="string")
*/
private $location;
/**
* #ORM\ManyToOne(targetEntity="Lookup\Entity\Fullhistory\SchemaInfo", cascade={"all"}, fetch="EAGER")
* #ORM\JoinColumn(name="schema_id", referencedColumnName="schema_id")
*/
private $schemaInfo;
/**
* #ORM\ManyToOne(targetEntity="Lookup\Entity\Intelligence\StandardDescribedUK", cascade={"all"}, fetch="EAGER")
* #ORM\JoinColumn(name="schema_id", referencedColumnName="schema_id")
*/
private $standardDescribed;
/**
* #var string
*
* #ORM\Column(type="string")
*/
private $data_value;
public function getVehicleId()
{
return $this->vehicle_id;
}
public function getSchemaId()
{
return $this->schema_id;
}
public function getOptionId()
{
return $this->option_id;
}
public function getRecordId()
{
return $this->record_id;
}
public function getLocation()
{
return $this->location;
}
public function getSchemaInfo()
{
return $this->schemaInfo;
}
public function getStandardDescribed()
{
return $this->standardDescribed;
}
public function getDataValue()
{
return $this->data_value;
}
}
SchemaInfo:
namespace Lookup\Entity\Fullhistory;
use Doctrine\ORM\Mapping as ORM;
/**
* SchemaInfo
*
* #ORM\Table(name="schema_info")
* #ORM\Entity
*/
class SchemaInfo
{
/**
* #var integer
*
* #ORM\Column(name="schema_id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $schema_id;
/**
* #var integer
*
* #ORM\Column(name="parent_schema_id", type="integer", nullable=true)
*/
private $parent_schema_id;
/**
* #var integer
*
* #ORM\Column(name="location_schema_id", type="integer", nullable=true)
*/
private $locationSchemaId;
/**
* #var integer
*
* #ORM\Column(name="scale_of_data", type="smallint", nullable=true)
*/
private $scaleOfData;
/**
* #var integer
*
* #ORM\Column(name="data_type", type="smallint", nullable=true)
*/
private $dataType;
public function getSchemaId()
{
return $this->schema_id;
}
public function getParentSchemaId()
{
return $this->parent_schema_id;
}
}
StandardDescribedUK
namespace Lookup\Entity\Intelligence;
use Doctrine\ORM\Mapping as ORM;
/**
* StandardDescribedUK
*
* #ORM\Table(name="jato_uk_intelligence.standard_described")
* #ORM\Entity
*/
class StandardDescribedUK
{
/**
* #var integer
*
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $schema_id;
/**
* #var string
*
* #ORM\Column(name="category", type="string", nullable=true)
*/
private $category;
public function getCategory()
{
return $this->category;
}
}
Your joins should use Doctrine's own relational schema: your EquipmentUK has declared its relation with SchemaInfo via the property $schemaInfo, and it has also declared its relation with StandardDescribedUK via $standardDescribed. So:
$qb->select('e')
->from('\Lookup\Entity\Fullhistory\EquipmentUK', 'e')
->leftJoin(
'e.schemaInfo',
'si'
//we don't need that third param as the relation is already known
)
->leftJoin(
'e.standardDescribed',
'sd'
//same here, no need for explicit relation
)
->where($qb->expr()->eq('e.vehicle_id', ':vehicle'))
//this here is a restriction, should be in WHERE instead of ON
->andWhere($qb->expr()->neq('si.parent_schema_id', 'si.schema_id'))
->setParameter('vehicle', $jatoid);
Hope this helps!

Doctrine 2: Update Entity Using Table Schema

I am using doctrine 2 within zend framework 2. To generate methods from existing entities using database table, the console command used is:
php doctrine-module orm:generate-entities --generate-annotations="true" --generate-methods="true" module
I have two namespaces Blog and Location
My question is:
1. When I run above code, only blog entities get updated. I want to know why it is behaving like this?
2. If I want to update only a specific entity, how can i do it?
The blog has two entities: Post and cateogry
Category.php
<?php
namespace Blog\Entity;
use Doctrine\ORM\Mapping as ORM;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
/**
* Category
*
* #ORM\Table(name="Categories")
* #ORM\Entity
*/
class Category implements InputFilterAwareInterface
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", nullable=false)
*/
private $name;
protected $inputFilter;
/**
* Get Id
*
* #param integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Category
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
}
Post.php
namespace Blog\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Post
*
* #ORM\Table(name="posts")
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
*/
class Post
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="title", type="string", precision=0, scale=0, nullable=false, unique=false)
*/
private $title;
/**
* #var string
*
* #ORM\Column(name="content", type="text", precision=0, scale=0, nullable=false, unique=false)
*/
private $content;
/**
* #var \DateTime
*
* #ORM\Column(name="created_date", type="datetime", precision=0, scale=0, nullable=false, unique=false)
*/
private $createdDate;
/**
* #var \Blog\Entity\Category
*
* #ORM\ManyToOne(targetEntity="Blog\Entity\Category")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="category_id", referencedColumnName="id", nullable=true)
* })
*/
private $category;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
* #return Post
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set content
*
* #param string $content
* #return Post
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* #return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set createdDate
*
* #param \DateTime $createdDate
* #return Post
*/
public function setCreatedDate($createdDate)
{
$this->createdDate = $createdDate;
return $this;
}
/**
* Get createdDate
*
* #return \DateTime
*/
public function getCreatedDate()
{
return $this->createdDate;
}
/**
* Set category
*
* #param \Blog\Entity\Category $category
* #return Post
*/
public function setCategory(\Blog\Entity\Category $category = null)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* #return \Blog\Entity\Category
*/
public function getCategory()
{
return $this->category;
}
}
The Location has Country.php
<?php
namespace Country\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Country
*
* #ORM\Table(name="countries")
* #ORM\Entity
*/
class Country
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=55, nullable=false)
*/
private $name;
}
OF Course everyone need a function on ZF2 that's turns to possible you generate a single entity. but those times you can't do it just by doctrine-orm-module. When you try to run doctrine in Symphony it's acceptable. I'm using doctrine2 with zf2 having the same issue.
I create a PHP Script that I call
Script to Create a Single Entity:
Choose Entity Name.
Generate All Entities on a Temp Folder.
Exclude All non-needed entities from folder.
Copy Single Entity to "src/$module/Entity" Folder.
Is a hack that i got to make it work property as you need.
Use the --filter option to do this.
php doctrine-module orm:generate-entities --filter="Post" ...

Categories