Reputation: 121
In my code I have a table called Squares. A Square has many Assets. Squares has a column called msid, which is a squares id.
In my Assets table I have a field called msid. This is the msid of the Square it belongs to.
I'm trying to set up a One-to-Many/Many-to-One relationship, and it's just not working. I have no idea what it is, and I'm very new to symfony and Doctrine, so if you think of a solution, please don't skip steps.
Thanks in advance, this is turning me off from this migration so fast.
EDIT: I need to mention that I am not generating a new schema. I am migrating a schema currently in production to ORM.
Assets.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Assets
*
* @ORM\Table(name="assets")
* @ORM\Entity
*/
class Assets
{
/**
* @ORM\ManyToOne(targetEntity="Squares", inversedBy="assets")
*/
private $square;
/**
* @var integer
*
* @ORM\Column(name="msid", type="integer", nullable=false)
*/
private $msid;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=64, nullable=false)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="type", type="string", length=32, nullable=false)
*/
private $type;
/**
* @var string
*
* @ORM\Column(name="data", type="string", length=4096, nullable=false)
*/
private $data;
/**
* @var integer
*
* @ORM\Column(name="assetid", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $assetid;
/**
* Set msid
*
* @param integer $msid
*
* @return Assets
*/
public function setMsid($msid)
{
$this->msid = $msid;
return $this;
}
/**
* Get msid
*
* @return integer
*/
public function getMsid()
{
return $this->msid;
}
/**
* Set name
*
* @param string $name
*
* @return Assets
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set type
*
* @param string $type
*
* @return Assets
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Set data
*
* @param string $data
*
* @return Assets
*/
public function setData($data)
{
$this->data = $data;
return $this;
}
/**
* Get data
*
* @return string
*/
public function getData()
{
return $this->data;
}
/**
* Get assetid
*
* @return integer
*/
public function getAssetid()
{
return $this->assetid;
}
}
Assets.SQL
CREATE TABLE IF NOT EXISTS `assets` (
`assetid` int(5) NOT NULL AUTO_INCREMENT,
`msid` int(5) NOT NULL,
`name` varchar(64) NOT NULL,
`type` varchar(32) NOT NULL,
`data` varchar(4096) NOT NULL,
PRIMARY KEY (`assetid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=8092 ;
Squares.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Squares
*
* @ORM\Table(name="squares")
* @ORM\Entity(repositoryClass="AppBundle\Repository\SquaresRepository")
*/
class Squares
{
/**
* @var integer
*
* @ORM\Column(name="userid", type="integer", nullable=false)
*/
private $userid;
/**
* @var boolean
*
* @ORM\Column(name="squaretype", type="boolean", nullable=false)
*/
private $squaretype;
/**
* @var \DateTime
*
* @ORM\Column(name="published", type="datetime", nullable=false)
*/
private $published = 'CURRENT_TIMESTAMP';
/**
* @var integer
*
* @ORM\Column(name="msid", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $msid;
/**
* @ORM\OneToMany(targetEntity="Assets", mappedBy="square")
*/
private $assets;
public function __construct()
{
$this->assets = new ArrayCollection();
}
// other getters setters here ...
/**
* Get assets
*
* @return ArrayCollection
*/
public function getAssets()
{
return $this->assets;
}
/**
* Set assets
*
* @param ArrayCollection $assets
*
* @return Squares
*/
public function setAssets($assets)
{
$this->assets = $assets;
return $this;
}
Squares SQL
CREATE TABLE IF NOT EXISTS `squares` (
`msid` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(5) NOT NULL,
`squaretype` tinyint(4) NOT NULL,
`published` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`package` text NOT NULL,
`counter` int(10) unsigned NOT NULL DEFAULT '0',
`firstname` varchar(20) NOT NULL,
`middlename` varchar(20) NOT NULL,
`lastname` varchar(20) NOT NULL,
`age` tinyint(1) DEFAULT NULL,
`dob` text NOT NULL,
`dod` text NOT NULL,
`city` varchar(32) NOT NULL,
`state` varchar(13) NOT NULL,
`zip` int(5) NOT NULL,
`sex` varchar(1) NOT NULL,
`bio` text NOT NULL,
`service` text NOT NULL,
`picture` int(11) DEFAULT NULL,
`video` text,
`videoexp` date DEFAULT NULL,
`videoReady` tinyint(1) NOT NULL,
`videoCounter` int(10) unsigned NOT NULL DEFAULT '0',
`vidIntro` text,
`vidMusic` text,
`vidBackground` text,
`dualfirst` varchar(20) NOT NULL,
`dualmiddle` varchar(20) NOT NULL,
`duallast` varchar(20) NOT NULL,
`dualdob` text NOT NULL,
`dualdod` text NOT NULL,
`dualpicture` int(11) DEFAULT NULL,
`couplesname` varchar(50) NOT NULL,
`birthday1` text,
`birthday2` text,
`visible` tinyint(4) NOT NULL DEFAULT '0',
`verified` tinyint(1) NOT NULL,
`fhName` varchar(256) NOT NULL,
`fhPhone` varchar(20) NOT NULL,
`fhLink` varchar(128) NOT NULL,
`clientid` int(4) unsigned zerofill NOT NULL,
PRIMARY KEY (`msid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10364 ;
Upvotes: 1
Views: 2055
Reputation: 121
This problem (I think) originated from the fact that I generated *.orm.xml files from an existing database, and tried to migrate automatically. While these .orm.xml files were in existence, I couldn't change anything. These files were located in AppBundle/Resources/config/doctrine... Once I deleted these, everything worked as I expected.
Upvotes: 0
Reputation: 118
First of all, you should check out the Doctrine website where it is explained.
Second of all, you should name your classes as a single object, which means name them Square instead of Squares and Asset instead of Assets.
You can leave out the msid column since Doctrine does the mapping for you by using the columns on which you put a mapping/reversedby property.
Upvotes: 0
Reputation: 397
I've taken the official example and adapted it to your naming:
/** @Entity */
class Square
{
// ...
/**
* @OneToMany(targetEntity="Asset", mappedBy="square")
*/
private $assets;
// ...
public function __construct() {
$this->assets = new ArrayCollection();
}
}
/** @Entity */
class Asset
{
// ...
/**
* @ManyToOne(targetEntity="Square", inversedBy="assets")
*/
private $square;
// ...
}
See how the mappedBy
and inversedBy
options point to the field names of the related entities and notice how the associations do not refer to the actual ids of the entities at all. If you want to control the name of the foreign key column in the table that will be generated for the Asset entity, you can add an @JoinColumn annotation, like in the official example. But I suggest you try it without one at first and observe what doctrine is generating for you.
Upvotes: 1