Starring for this for several hours now, maybe I missed something obvious.
Have this database structure (with indexes, and constraints)
CREATE TABLE `exploit` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`edb_id` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`date` datetime not null,
`author` bigint(20) not null ,
`name` varchar(255) not null,
`category` bigint(20) not null,
`version` varchar(255) not null,
`type` bigint(20) not null,
`content` longtext COLLATE utf8_unicode_ci NOT NULL,
`dork` varchar(255) null,
`software_link` varchar(255) null,
`tested_on` varchar(255) null,
PRIMARY KEY (`id`),
KEY `exploit_category_idx` (`category`),
KEY `exploit_type_idx` (`type`),
KEY `exploit_author_idx` (`author`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `category` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `category_name_id_idx` (`id`),
CONSTRAINT `category_name_id` FOREIGN KEY (`id`) REFERENCES `exploit` (`category`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `type` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `type_name_id_idx` (`id`),
CONSTRAINT `type_name_id` FOREIGN KEY (`id`) REFERENCES `exploit` (`type`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `author` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `author_name_id_idx` (`id`),
CONSTRAINT `author_name_id` FOREIGN KEY (`id`) REFERENCES `exploit` (`author`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Created these entities:
::::::::::::::
Author.php
::::::::::::::
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Exploit;
/**
* Author
*
* #ORM\Table(name="author", indexes={#ORM\Index(name="author_name_id_idx", columns={"id"})})
* #ORM\Entity
*/
class Author
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", author="string", length=255, nullable=false)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Exploit", mappedBy="author", cascade={"persist", "remove"})
* #ORM\JoinColumn(name="exploits", referencedColumnName="id")
*/
private $exploits;
/**
* Author constructor.
*/
public function __construct()
{
$this->exploits = new ArrayCollection();
}
/**
* Set name
*
* #param string $name
*
* #return Author
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Get id
*
*/
public function getId()
{
return $this->id;
}
public function __toString()
{
return $this->name;
}
/**
* #param Exploits $exploit
*
* #return Author
*/
public function addExploit($exploit)
{
$this->exploits->add($exploit);
return $this;
}
/**
* #param Collection $exploits
*
* #return Author
*/
public function setExploits(Collection $exploits)
{
$this->exploits->clear();
foreach ($exploits as $exploit) {
$exploit->add($this);
}
$this->exploits = $exploits;
return $this;
}
}
::::::::::::::
Category.php
::::::::::::::
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Exploit;
/**
* Category
*
* #ORM\Table(name="category", indexes={#ORM\Index(name="category_name_id_idx", columns={"id"})})
* #ORM\Entity
*/
class Category
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", category="string", length=255, nullable=false)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Exploit", mappedBy="category", cascade={"persist", "remove"})
* #ORM\JoinColumn(name="exploits", referencedColumnName="id")
*/
private $exploits;
/**
* Author constructor.
*/
public function __construct()
{
$this->exploits = new ArrayCollection();
}
/**
* Set name
*
* #param string $name
*
* #return Author
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Get id
*
*/
public function getId()
{
return $this->id;
}
public function __toString()
{
return $this->name;
}
/**
* #param Exploits $exploit
*
* #return Author
*/
public function addExploit($exploit)
{
$this->exploits->add($exploit);
return $this;
}
/**
* #param Collection $exploits
*
* #return Author
*/
public function setExploits(Collection $exploits)
{
$this->exploits->clear();
foreach ($exploits as $exploit) {
$exploit->add($this);
}
$this->exploits = $exploits;
return $this;
}
}
::::::::::::::
Exploit.php
::::::::::::::
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Author;
use AppBundle\Entity\Type;
use AppBundle\Entity\Category;
/**
* Exploit
*
* #ORM\Table(name="exploit", indexes={#ORM\Index(name=exploit_category_idx", columns={"category"}), #ORM\Index(name="exploit_type_idx", columns={"type"}), #ORM\Index(name="exploit_author_idx", columns={"
author"})})
*/
class Exploit
{
/**
* #var integer
*
* #ORM\Column(name="id", type="bigint", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="edb_id", type="string", length=100, nullable=false)
*/
private $edbId;
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="datetime", nullable=false)
*/
private $date;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Author", inversedBy="exploits")
* #ORM\JoinColumn(name="author", referencedColumnName="id")
*/
private $author;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* #var integer
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Category", inversedBy="exploits")
* #ORM\JoinColumn(name="category", referencedColumnName="id")
*/
private $category;
/**
* #var string
*
* #ORM\Column(name="version", type="string", length=255, nullable=false)
*/
private $version;
/**
* #var integer
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Type", inversedBy="exploits")
* #ORM\JoinColumn(name="type", referencedColumnName="id")
*/
private $type;
/**
* #var string
*
* #ORM\Column(name="content", type="text", nullable=false)
*/
private $content;
/**
* #var string
*
* #ORM\Column(name="dork", type="string", length=255, nullable=true)
*/
private $dork;
/**
* #var string
*
* #ORM\Column(name="software_link", type="string", length=255, nullable=true)
*/
private $softwareLink;
/**
* #var string
*
* #ORM\Column(name="tested_on", type="string", length=255, nullable=true)
*/
private $testedOn;
/**
* Set edbId
*
* #param integer $edbId
*
* #return Exploit
*/
public function setEdbId($edbId)
{
$this->edbId = $edbId;
return $this;
}
/**
* Get edbId
*
* #return integer
*/
public function getEdbId()
{
return $this->edbId;
}
/**
* Set date
*
* #param \DateTime $date
*
* #return Exploit
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* #return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set author
*
* #param integer $author
*
* #return Exploit
*/
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* #return integer
*/
public function getAuthor()
{
return $this->author;
}
/**
* Set name
*
* #param string $name
*
* #return Exploit
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set category
*
* #param integer $category
*
* #return Exploit
*/
public function setCategory($category)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* #return integer
*/
public function getCategory()
{
return $this->category;
}
/**
* Set version
*
* #param string $version
*
* #return Exploit
*/
public function setVersion($version)
{
$this->version = $version;
return $this;
}
/**
* Get version
*
* #return string
*/
public function getVersion()
{
return $this->version;
}
/**
* Set type
*
* #param integer $type
*
* #return Exploit
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* #return integer
*/
public function getType()
{
return $this->type;
}
/**
* Set content
*
* #param string $content
*
* #return Exploit
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* #return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set dork
*
* #param string $dork
*
* #return Exploit
*/
public function setDork($dork)
{
$this->dork = $dork;
return $this;
}
/**
* Get dork
*
* #return string
*/
public function getDork()
{
return $this->dork;
}
/**
* Set softwareLink
*
* #param string $softwareLink
*
* #return Exploit
*/
public function setSoftwareLink($softwareLink)
{
$this->softwareLink = $softwareLink;
return $this;
}
/**
* Get softwareLink
*
* #return string
*/
public function getSoftwareLink()
{
return $this->softwareLink;
}
/**
* Set testedOn
*
* #param string $testedOn
*
* #return Exploit
*/
public function setTestedOn($testedOn)
{
$this->testedOn = $testedOn;
return $this;
}
/**
* Get testedOn
*
* #return string
*/
public function getTestedOn()
{
return $this->testedOn;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
::::::::::::::
Type.php
::::::::::::::
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Exploit;
/**
* Type
*
* #ORM\Table(name="type", indexes={#ORM\Index(name="type_name_id_idx", columns={"id"})})
* #ORM\Entity
*/
class Type
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Exploit", mappedBy="type", cascade={"persist", "remove"})
* #ORM\JoinColumn(name="exploits", referencedColumnName="id")
*/
private $exploits;
/**
* Type constructor.
*/
public function __construct()
{
$this->exploits = new ArrayCollection();
}
/**
* Set name
*
* #param string $name
*
* #return Type
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Get id
*
*/
public function getId()
{
return $this->id;
}
public function __toString()
{
return $this->name;
}
/**
* #param Exploits $exploit
*
* #return Type
*/
public function addExploit($exploit)
{
$this->exploits->add($exploit);
return $this;
}
/**
* #param Collection $exploits
*
* #return Type
*/
public function setExploits(Collection $exploits)
{
$this->exploits->clear();
foreach ($exploits as $exploit) {
$exploit->add($this);
}
$this->exploits = $exploits;
return $this;
}
}
but somehow when I make a query I get, like:
$exploits = $this->getDoctrine()
->getRepository('AppBundle:Exploit')
->findAll();
and in view
<th scope="row"> {{ exploit.id }} </th>
<td> {{ exploit.name }} </td>
<td> {{ exploit.author.name }} </td>
<td> {{ exploit.type.name }} </td>
<td> {{ exploit.category.name }} </td>
<td>{{ exploit.date|date('F j, Y, g:i a') }}</td>
I get this error:
Impossible to access an attribute ("name") on a integer variable ("1").
Any good soul can look into this and try to reproduce it?
MySQL Dump to recreate the tables with content is here:
https://0bin.net/paste/2tV3MEw4A2tdAVsR#R3rBNW4seWkK9HtlJFwbsA6+RmhhWPilm40L8QfeiTp
Thanks!
It says it right there, you're trying to access the name attribute of something, but that something isn't an object. Its a 1.
You access .name several times so unsure which one of these it is without more info and a line number
<th scope="row"> {{ exploit.id }} </th>
<td> {{ exploit.name }} </td>
<td> {{ exploit.author.name }} </td>
<td> {{ exploit.type.name }} </td>
<td> {{ exploit.category.name }} </td>
<td>{{ exploit.date|date('F j, Y, g:i a') }}</td>
</th>
But if we assume its Author then its clear that the getAuthor() method returns a bigint.
How did you get that entity code and the database code? Because they do not look correct, they are getting and setting integers, rather than objects.
UPDATE
Generate entities with
php bin/console doctrine:generate:entities AppBundle
View the SQL using
php bin/console doctrine:schema:update --dump-sql
Execute the SQL using
php bin/console doctrine:schema:update --force
Got it working with these entities:
::::::::::::::
Author.php
::::::::::::::
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Exploit;
/**
* User
*
* #ORM\Table(name="author")
* #ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class Author
{
/**
* #var integer
*
* #ORM\Column(name="a_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $a_id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="Exploit", mappedBy="author")
*/
protected $exploits;
public function __construct()
{
$this->exploits = new ArrayCollection();
}
public function addExploit(\AppBundle\Entity\Exploit $exploit)
{
$this->report[] = $exploit;
}
public function getExploits()
{
return $this->exploits;
}
/**
* Set name
*
* #param string $name
*
* #return Type
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
}
::::::::::::::
Category.php
::::::::::::::
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Exploit;
/**
* Category
*
* #ORM\Table(name="category")
* #ORM\Entity(repositoryClass="AppBundle\Repository\CategoryRepository")
*/
class Category
{
/**
* #var integer
*
* #ORM\Column(name="c_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $c_id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="Exploit", mappedBy="category")
*/
protected $exploits;
public function __construct()
{
$this->exploits = new ArrayCollection();
}
public function addExploit(\AppBundle\Entity\Exploit $exploit)
{
$this->report[] = $exploit;
}
public function getExploits()
{
return $this->exploits;
}
/**
* Set name
*
* #param string $name
*
* #return Type
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
}
::::::::::::::
Exploit.php
::::::::::::::
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Author;
/**
* Exploit
*
* #ORM\Table(name="exploit")
* #ORM\Entity(repositoryClass="AppBundle\Repository\ReportRepository")
*/
class Exploit
{
/**
* #var integer
*
* #ORM\Column(name="e_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $e_id;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Author", inversedBy="exploi
ts")
* #ORM\JoinColumn(name="author_id", referencedColumnName="a_id")
*/
protected $author;
public function setAuthor(\AppBundle\Entity\Author $author)
{
$this->author = $author;
}
public function getAuthor()
{
return $this->author;
}
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Category", inversedBy="expl
oits")
* #ORM\JoinColumn(name="category_id", referencedColumnName="c_id")
*/
protected $category;
public function setCategory(\AppBundle\Entity\Category $category)
{
$this->category = $category;
}
public function getCategory()
{
return $this->category;
}
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Type", inversedBy="exploits
")
* #ORM\JoinColumn(name="type_id", referencedColumnName="t_id")
*/
protected $type;
public function setType(\AppBundle\Entity\Type $type)
{
$this->type = $type;
}
public function getType()
{
return $this->type;
}
/**
* #var string
*
* #ORM\Column(name="edb_id", type="string", length=100, nullable=false)
*/
private $edbId;
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="datetime", nullable=false)
*/
private $date;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="version", type="string", length=255, nullable=false)
*/
private $version;
/**
* #var string
*
* #ORM\Column(name="content", type="text", nullable=false)
*/
private $content;
/**
* #var string
*
* #ORM\Column(name="dork", type="string", length=255, nullable=true)
*/
private $dork;
/**
* #var string
*
* #ORM\Column(name="software_link", type="string", length=255, nullable=tru
e)
*/
private $softwareLink;
/**
* #var string
*
* #ORM\Column(name="tested_on", type="string", length=255, nullable=true)
*/
private $testedOn;
/**
* Set edbId
*
* #param integer $edbId
*
* #return Exploit
*/
public function setEdbId($edbId)
{
$this->edbId = $edbId;
return $this;
}
/**
* Get edbId
*
* #return integer
*/
public function getEdbId()
{
return $this->edbId;
}
/**
* Set date
*
* #param \DateTime $date
*
* #return Exploit
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* #return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set name
*
* #param string $name
*
* #return Exploit
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set version
*
* #param string $version
*
* #return Exploit
*/
public function setVersion($version)
{
$this->version = $version;
return $this;
}
/**
* Get version
*
* #return string
*/
public function getVersion()
{
return $this->version;
}
/**
* Set content
*
* #param string $content
*
* #return Exploit
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* #return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set dork
*
* #param string $dork
*
* #return Exploit
*/
public function setDork($dork)
{
$this->dork = $dork;
return $this;
}
/**
* Get dork
*
* #return string
*/
public function getDork()
{
return $this->dork;
}
/**
* Set softwareLink
*
* #param string $softwareLink
*
* #return Exploit
*/
public function setSoftwareLink($softwareLink)
{
$this->softwareLink = $softwareLink;
return $this;
}
/**
* Get softwareLink
*
* #return string
*/
public function getSoftwareLink()
{
return $this->softwareLink;
}
/**
* Set testedOn
*
* #param string $testedOn
*
* #return Exploit
*/
public function setTestedOn($testedOn)
{
$this->testedOn = $testedOn;
return $this;
}
/**
* Get testedOn
*
* #return string
*/
public function getTestedOn()
{
return $this->testedOn;
}
/**
* Get e_id
*
* #return integer
*/
public function gete_id()
{
return $this->e_id;
}
}
::::::::::::::
Type.php
::::::::::::::
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Exploit;
/**
* Type
*
* #ORM\Table(name="type")
* #ORM\Entity(repositoryClass="AppBundle\Repository\TypeRepository")
*/
class Type
{
/**
* #var integer
*
* #ORM\Column(name="t_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $t_id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="Exploit", mappedBy="type")
*/
protected $exploits;
public function __construct()
{
$this->exploits = new ArrayCollection();
}
public function addExploit(\AppBundle\Entity\Exploit $exploit)
{
$this->report[] = $exploit;
}
public function getExploits()
{
return $this->exploits;
}
/**
* Set name
*
* #param string $name
*
* #return Type
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
}
This view:
{% for exploit in exploits %}
<tr>
<th scope="row">{{ exploit.e_id }}</th>
<td>{{ exploit.name }}</td>
<td> {{ exploit.author.name }} </td>
<td> {{ exploit.category.name }} </td>
<td> {{ exploit.type.name }} </td>
<td>
View
Edit
Delete
</td>
</tr>
{% endfor %}
And controller:
$exploits = $this->getDoctrine()
->getRepository('AppBundle:Exploit')
->findAll();
return $this->render('exploit/index.html.twig', array(
'exploits' => $exploits
));
I've been reviewing the Integrity constraint violation issues here on Stackoverflow for the past few days trying to resolve this issue, but I'm not having any success deducing the proper doctrine logic.
I have two entities that express a one-to-many and one-to-many relationship, but when I try to add and persist an item I get a insert error:
(in controller)
$user = $this->get('security.token_storage')->getToken()->getUser();
$chartnew = $Charts->setChartNo(9999);
$user->addUserschart($chartnew);
$em->persist($user);
$em->flush();
an exception occurred while executing 'INSERT INTO charts (chart_no, id_user) VALUES (?, ?)' with params [9999, null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'id_user' cannot be null
class Users implements UserInterface
{
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Charts",cascade={"persist"}, mappedBy="charts")
*/
private $userscharts;
public function __construct()
{
$this->userscharts = new ArrayCollection();
//parent::__construct();
}
/**
* Add userschart
*
* #param \AppBundle\Entity\Charts $userschart
*
* #return Users
*/
public function addUserschart(\AppBundle\Entity\Charts $userschart)
{
$this->userscharts->add($userschart);
return $this;
}
.......
}
class Charts
{
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Users", inversedBy="userscharts")
* #ORM\JoinColumn(name="id_user", referencedColumnName="id_user")
*/
private $charts;
/**
* #var integer
*
* #ORM\Column(name="chart_no", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $chartNo;
/**
* #var integer
*
* #ORM\Column(name="id_user", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
*/
private $idUser;
/**
* Set chartNo
*
* #param integer $chartNo
*
* #return Charts
*/
public function setChartNo($chartNo)
{
$this->chartNo = $chartNo;
return $this;
}
/**
* Get chartNo
*
* #return integer
*/
public function getChartNo()
{
return $this->chartNo;
}
/**
* Set idUser
*
* #param integer $idUser
*
* #return Charts
*/
public function setIdUser($idUser)
{
$this->idUser = $idUser;
return $this;
}
/**
* Get idUser
*
* #return integer
*/
public function getIdUser()
{
return $this->idUser;
}
/**
* Set charts
*
* #param \AppBundle\Entity\Users $charts
*
* #return Charts
*/
public function setCharts(\AppBundle\Entity\Users $charts = null)
{
$this->charts = $charts;
return $this;
}
/**
* Get charts
*
* #return \AppBundle\Entity\Users
*/
public function getCharts()
{
return $this->charts;
}
}
So, my problem is when I'm trying to update schema with command "dotrine:schema:update --force", entity are create but without any relation.
I have two entity "advert" and "category" and a relation ManyToMany on advert entity.
This is entyty advert :
<?php
namespace testBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Advert
*
* #ORM\Table(name="advert")
* #ORM\Entity(repositoryClass="testBundle\Repository\AdvertRepository")
*/
class Advert
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="categories", type="string", length=255)
* /**
* #ORM\ManyToMany(targetEntity="OC\PlatformBundle\Entity\Category", cascade={"persist"})
*/
private $categories;
public function __construct()
{
$this->date = new \Datetime();
$this->categories = new ArrayCollection();
}
// Notez le singulier, on ajoute une seule catégorie à la fois
public function addCategory(Category $category)
{
// Ici, on utilise l'ArrayCollection vraiment comme un tableau
$this->categories[] = $category;
}
public function removeCategory(Category $category)
{
// Ici on utilise une méthode de l'ArrayCollection, pour supprimer la catégorie en argument
$this->categories->removeElement($category);
}
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set categories
*
* #param string $categories
*
* #return Advert
*/
public function setCategories($categories)
{
$this->categories = $categories;
return $this;
}
/**
* Get categories
*
* #return string
*/
public function getCategories()
{
return $this->categories;
}
}
My entity category :
<?php
namespace testBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Category
*
* #ORM\Table(name="category")
* #ORM\Entity(repositoryClass="testBundle\Repository\CategoryRepository")
*/
class Category
{
/**
* #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)
*/
private $name;
/**
* Get id
*
* #return int
*/
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;
}
}
and this is the result of command doctrine:schema:update --force --dump-sql :
CREATE TABLE advert (id INT AUTO_INCREMENT NOT NULL, categories VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE category (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
Like you can see, no relation and no table advert_category
I found some topic about this but no solution working for me.
I hope someone can help me
Thank you !
Relation declared wrong, i think it should be something like this :
/**
* #ORM\ManyToMany(targetEntity="testBundle\Entity\Category")
* #ORM\JoinTable(name="advert_category")
*/
private $categories;
So problem fixed, I have to delete this :
*
* #ORM\Column(name="categories", type="string", length=255)
/**
and it's work , thank you #thomas !
I had a problem like here. I followed the instructions of given solutions, but it generate another problem.
I've got two Entities:
namespace Acme\TyperBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Mecz
*
* #ORM\Table()
* #ORM\Entity
*/
class Mecz
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="druzyna1", type="string", length=255)
*/
private $druzyna1;
/**
* #var string
*
* #ORM\Column(name="druzyna2", type="string", length=255)
*/
private $druzyna2;
/**
* #var int
*
* #ORM\Column(name="bramki1", type="integer")
*/
private $bramki1;
/**
* #var int
*
* #ORM\Column(name="bramki2", type="integer")
*/
private $bramki2;
/**
* #var string
*
* #ORM\Column(name="wyniktyp", type="string", length=2)
*/
private $wyniktyp;
/**
* #var \DateTime
*
* #ORM\Column(name="data", type="datetime")
*/
private $data;
/**
* #var int
*
* #ORM\Column(name="typ1", type="float")
*/
private $typ1;
/**
* #var int
*
* #ORM\Column(name="typ1x", type="float")
*/
private $typ1x;
/**
* #var int
*
* #ORM\Column(name="typ2", type="float")
*/
private $typ2;
/**
* #var int
*
* #ORM\Column(name="typ2x", type="float")
*/
private $typ2x;
/**
* #var int
*
* #ORM\Column(name="typx", type="float")
*/
private $typx;
/**
* #ORM\OneToMany(targetEntity="Typy", mappedBy="meczid")
*/
protected $meczid;
public function __construct()
{
$this->products = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set druzyna1
*
* #param string $druzyna1
* #return Mecz
*/
public function setDruzyna1($druzyna1)
{
$this->druzyna1 = $druzyna1;
return $this;
}
/**
* Get druzyna1
*
* #return string
*/
public function getDruzyna1()
{
return $this->druzyna1;
}
/**
* Set druzyna2
*
* #param string $druzyna2
* #return Mecz
*/
public function setDruzyna2($druzyna2)
{
$this->druzyna2 = $druzyna2;
return $this;
}
/**
* Get druzyna2
*
* #return string
*/
public function getDruzyna2()
{
return $this->druzyna2;
}
/**
* Set bramki1
*
* #param integer $bramki1
* #return Mecz
*/
public function setBramki1($bramki1)
{
$this->bramki1 = $bramki1;
return $this;
}
/**
* Get bramki1
*
* #return integer
*/
public function getBramki1()
{
return $this->bramki1;
}
/**
* Set bramki2
*
* #param integer $bramki2
* #return Mecz
*/
public function setBramki2($bramki2)
{
$this->bramki2 = $bramki2;
return $this;
}
/**
* Get bramki2
*
* #return integer
*/
public function getBramki2()
{
return $this->bramki2;
}
/**
* Set wyniktyp
*
* #param string $wyniktyp
* #return Mecz
*/
public function setWyniktyp($wyniktyp)
{
$this->wyniktyp = $wyniktyp;
return $this;
}
/**
* Get wyniktyp
*
* #return string
*/
public function getWyniktyp()
{
return $this->wyniktyp;
}
/**
* Set data
*
* #param \DateTime $data
* #return Mecz
*/
public function setData($data)
{
$this->data = $data;
return $this;
}
/**
* Get data
*
* #return \DateTime
*/
public function getData()
{
return $this->data;
}
/**
* Set typ1
*
* #param float $typ1
* #return Mecz
*/
public function setTyp1($typ1)
{
$this->typ1 = $typ1;
return $this;
}
/**
* Get typ1
*
* #return float
*/
public function getTyp1()
{
return $this->typ1;
}
/**
* Set typ2
*
* #param float $typ2
* #return Mecz
*/
public function setTyp2($typ2)
{
$this->typ2 = $typ2;
return $this;
}
/**
* Get typ2
*
* #return float
*/
public function getTyp2()
{
return $this->typ2;
}
/**
* Set typ1x
*
* #param float $typ1x
* #return Mecz
*/
public function setTyp1x($typ1x)
{
$this->typ1x = $typ1x;
return $this;
}
/**
* Get typ1x
*
* #return float
*/
public function getTyp1x()
{
return $this->typ1x;
}
/**
* Set typ2x
*
* #param float $typ2x
* #return Mecz
*/
public function setTyp2x($typ2x)
{
$this->typ2x = $typ2x;
return $this;
}
/**
* Get typ2x
*
* #return float
*/
public function getTyp2x()
{
return $this->typ2x;
}
/**
* Set typx
*
* #param float $typx
* #return Mecz
*/
public function setTypx($typx)
{
$this->typx = $typx;
return $this;
}
/**
* Get typx
*
* #return float
*/
public function getTypx()
{
return $this->typx;
}
/**
* Set id
*
* #param integer $id
* #return Mecz
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Add meczid
*
* #param \Acme\TyperBundle\Entity\Typy $meczid
* #return Mecz
*/
public function addMeczid(\Acme\TyperBundle\Entity\Typy $meczid)
{
$this->meczid[] = $meczid;
return $this;
}
/**
* Remove meczid
*
* #param \Acme\TyperBundle\Entity\Typy $meczid
*/
public function removeMeczid(\Acme\TyperBundle\Entity\Typy $meczid)
{
$this->meczid->removeElement($meczid);
}
/**
* Get meczid
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getMeczid()
{
return $this->meczid;
}
/**
* Add mecz
*
* #param \Acme\TyperBundle\Entity\Typy $mecz
* #return Mecz
*/
public function addMecz(\Acme\TyperBundle\Entity\Typy $mecz)
{
$this->mecz[] = $mecz;
return $this;
}
/**
* Remove mecz
*
* #param \Acme\TyperBundle\Entity\Typy $mecz
*/
public function removeMecz(\Acme\TyperBundle\Entity\Typy $mecz)
{
$this->mecz->removeElement($mecz);
}
/**
* Get mecz
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getMecz()
{
return $this->mecz;
}
/**
* Add mecztyp
*
* #param \Acme\TyperBundle\Entity\Typy $mecztyp
* #return Mecz
*/
public function addMecztyp(\Acme\TyperBundle\Entity\Typy $mecztyp)
{
$this->mecztyp[] = $mecztyp;
return $this;
}
/**
* Remove mecztyp
*
* #param \Acme\TyperBundle\Entity\Typy $mecztyp
*/
public function removeMecztyp(\Acme\TyperBundle\Entity\Typy $mecztyp)
{
$this->mecztyp->removeElement($mecztyp);
}
/**
* Get mecztyp
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getMecztyp()
{
return $this->mecztyp;
}
}
and
namespace Acme\TyperBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Typy
*
* #ORM\Table()
* #ORM\Entity
*/
class Typy
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var integer
*
* #ORM\Column(name="id_kuponu", type="integer")
*/
private $id_kuponu;
/**
* #var integer
*
* #ORM\Column(name="id_meczu", type="integer")
*/
private $id_meczu;
/**
* #var string
*
* #ORM\Column(name="typ", type="string", length=2)
*/
private $typ;
/**
* #var int
*
* #ORM\Column(name="stawka", type="float")
*/
private $stawka;
/**
* #ORM\ManyToOne(targetEntity="Mecz", inversedBy="meczid",cascade={"persist"})
* #ORM\JoinColumn(name="id_meczu", referencedColumnName="id")
*/
private $meczid;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set id_kuponu
*
* #param integer $idKuponu
* #return Typy
*/
public function setIdKuponu($idKuponu)
{
$this->id_kuponu = $idKuponu;
return $this;
}
/**
* Get id_kuponu
*
* #return integer
*/
public function getIdKuponu()
{
return $this->id_kuponu;
}
/**
* Set id_meczu
*
* #param integer $idMeczu
* #return Typy
*/
public function setIdMeczu($idMeczu)
{
$this->id_meczu = $idMeczu;
return $this;
}
/**
* Get id_meczu
*
* #return integer
*/
public function getIdMeczu()
{
return $this->id_meczu;
}
/**
* Set typ
*
* #param string $typ
* #return Typy
*/
public function setTyp($typ)
{
$this->typ = $typ;
return $this;
}
/**
* Get typ
*
* #return string
*/
public function getTyp()
{
return $this->typ;
}
/**
* Set meczid
*
* #param \Acme\TyperBundle\Entity\Mecz $meczid
* #return Typy
*/
public function setMeczid(\Acme\TyperBundle\Entity\Mecz $meczid = null)
{
$this->meczid = $meczid;
return $this;
}
/**
* Get meczid
*
* #return \Acme\TyperBundle\Entity\Mecz
*/
public function getMeczid()
{
return $this->meczid;
}
/**
* Set stawka
*
* #param float $stawka
* #return Typy
*/
public function setStawka($stawka)
{
$this->stawka = $stawka;
return $this;
}
/**
* Get stawka
*
* #return float
*/
public function getStawka()
{
return $this->stawka;
}
}
So I want to use this code to insert data to table:
$mecz = new Mecz();
$mecz->setId($id_meczu);
$typs = new Typy();
$et = $this->getDoctrine()->getManager();
$typs->setIdKuponu($id_kuponu);
$typs->setMeczid($mecz);
$typs->setTyp($typ);
$typs->setStawka($stawka);
$et->persist($typs);
$et->flush();
And i get exception:
An exception occurred while executing 'INSERT INTO Mecz (id, druzyna1, druzyna2, bramki1, bramki2, wyniktyp, data, typ1, typ1x, typ2, typ2x, typx) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params ["2", null, null, null, null, null, null, null, null, null, null, null]:
I don't know why, because i want to instert data to "Typy" table not "Mecz" table. Can anyone could help me?
The problem is the following:
You have the field Typy::meczid, but this field does (from the perspecitve of Doctrine) not hold a numeric ID, but a reference to a Mecz instance. Therefore the name meczid is misleading, it should be Typy::mecz. I'd recommend renaming the field and the accessors for the sake of consistency.
Now the Typy entity expects a Mecz entity to be set, which you do: You create a new Mecz(), and assign it to Typy with $typs->setMeczid($mecz);. BUT: The Mecz instance is lacking lots all properties except the ID. (By the way, in Doctrine IDs should generally be autogenerated, not by the business logic.) As the Typy entity depends on Mecz, the Mecz must be persisted first, so its reference can be stored with the Typy.
Bottom line: You must fill the generated Mecz with the missing properties.
Hint: To avoid that this type of error happens on the DB level, you should use Symfony's entity validation component. This will allow a much better error handling; both during development, and in production.
I appear to be having a problem creating a simple One to Many relationship between my blog and my blog_link_tag join table. I'm almost there however I keep on receiving the following mapping error from Doctrine and I'm wondering if anyone can point out where I'm going wrong?
The association Acme\BlogBundle\Entity\Blog#tags refers to the owning side field Acme\BlogBundle\Entity\BlogLinkTag#blog_id which does not exist.
Below is the table structure I'm using with unnecessary fields removed.
CREATE TABLE `blog` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `blog_link_tag` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`blog_id` int(3) NOT NULL,
`tag_id` int(3) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Blog.php
<?php
namespace Acme\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Blog
*
* #ORM\Table(name="blog")
* #ORM\Entity(repositoryClass="Acme\BlogBundle\Entity\BlogRepository")
* #ORM\HasLifecycleCallbacks
*/
class Blog {
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #ORM\OneToMany(targetEntity="BlogLinkTag", mappedBy="blog_id")
*/
protected $tags;
public function __construct() {
$this->tags = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId() {
return $this->id;
}
/**
* Add tags
*
* #param \Acme\BlogBundle\Entity\BlogLinkTag $tags
* #return Blog
*/
public function addTag(\Acme\BlogBundle\Entity\BlogLinkTag $tags) {
$this->tags[] = $tags;
return $this;
}
/**
* Remove tags
*
* #param \Acme\BlogBundle\Entity\BlogLinkTag $tags
*/
public function removeTag(\Acme\BlogBundle\Entity\BlogLinkTag $tags) {
$this->tags->removeElement($tags);
}
/**
* Get tags
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTags() {
return $this->tags;
}
/**
* Set tags
*
* #param integer $tags
* #return Blog
*/
public function setTags($tags) {
$this->tags = $tags;
return $this;
}
}
BlogLinkTag.php
<?php
namespace Acme\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* BlogLinkTag
*
* #ORM\Table(name="blog_link_tag")
* #ORM\Entity
*/
class BlogLinkTag
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var integer
* #ORM\Column(name="blog_id", type="integer", nullable=false)
* #ORM\ManyToOne(targetEntity="Blog", inversedBy="blog")
* #ORM\JoinColumn(name="blog_id", referencedColumnName="blog_id")
*/
private $blogId;
/**
* #var integer
*
* #ORM\Column(name="tag_id", type="integer", nullable=false)
*/
private $tagId;
/**
* Get id
*
* #return integer
*/
public function getId() {
return $this->id;
}
/**
* Set blogId
*
* #param integer $blogId
* #return BlogLinkTag
*/
public function setBlogId($blogId) {
$this->blogId = $blogId;
return $this;
}
/**
* Get blogId
*
* #return integer
*/
public function getBlogId() {
return $this->blogId;
}
/**
* Set tagId
*
* #param integer $tagId
* #return BlogLinkTag
*/
public function setTagId($tagId) {
$this->tagId = $tagId;
return $this;
}
/**
* Get tagId
*
* #return integer
*/
public function getTagId() {
return $this->tagId;
}
}
Take a look at the official documentation about association mapping here.
Try this :
In BlogLinkTag
/**
* #var integer
* #ORM\ManyToOne(targetEntity="Blog", inversedBy="tags") //put the name of the variable in the other entity here
* #ORM\JoinColumn(name="blog_id", referencedColumnName="id") //reference of the column targetted here
*/
private $blog;
In Blog :
/**
* #ORM\OneToMany(targetEntity="BlogLinkTag", mappedBy="blog") //put the name of the variable in the other entity here
*/
protected $tags;