Symfony3-实体不适用于关系(具有索引和约束的数据库) [英] Symfony3 - Entities don't work with relations (database with indexes and constraints)

查看:45
本文介绍了Symfony3-实体不适用于关系(具有索引和约束的数据库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为此目的主演了几个小时,也许我错过了一些明显的事情。



具有这种数据库结构(带有索引和约束)

 创建表ʻexploit`(
`id` bigint(20)NOT NULL AUTO_INCREMENT,
`edb_id` varchar(100)COLLATE utf8_unicode_ci NOT NULL,
`date`日期时间不为空,
ʻauthor` bigint(20)不为空,
`name` varchar(255)不为空,
`category` bigint(20)不为null,
`version` varchar(255)不为空,
`type` bigint(20)不为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`),
键ʻexploit_category_idx`(类别),
键ʻexploit_type_idx`(类型),
键ʻexploit_author_idx`(作者)
)ENGINE = InnoDB AUTO_INCREMENT = 1个默认字符集= utf8 COLLATE = utf8_unicode_ci;

创建表`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`外部密钥(ʻid`)参考DELOE CASTING(`category`)删除级联
)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密钥(ʻid`),
密钥`type_name_id_idx`(ʻid`),
约束`type_name_id`外部密钥(ʻid`)引用删除级联$ b上的`exploit`(`type`) $ b)ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;

创建表ʻauthor`(
ʻid` bigint(20)NOT NULL AUTO_INCREMENT,
`name` varchar(255)COLLATE utf8_unicode_ci NOT NULL,
PRIMARY密钥(ʻid`),
密钥`author_name_id_idx`(ʻid`),
约束`author_name_id`外部密钥(ʻid`)参考`exploit`(`author`)删除级联
)ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;

创建了以下实体:

  ::::::::::::: 
Author.php
:::::::::::::::
<?php
命名空间AppBundle\Entity;

使用Doctrine\ORM\Mapping作为ORM;
使用AppBundle\Entity\Exploit;

/ **
*作者
*
* @ ORM\Table(name = author,index = {@@ ORM\Index(name = author_name_id_idx,columns = { id})})
* @ ORM\Entity
* /
class作者
{

/ **
* @ ORM\Id
* @ ORM\GeneratedValue(strategy = NONE)
* /
private $ id;

/ **
* @var字符串
*
* @ ORM\Column(name = name,author = string,length = 255 ,nullable = false)
* /
私人$ name;


/ **
* @ ORM\OneToMany(targetEntity = AppBundle\Entity\Exploit,mappingBy = author,级联= { persist , remove})
* @ ORM\JoinColumn(name = exploits,referencedColumnName = id)
* /
private $ exploits;

/ **
*作者构造函数。
* /
公共函数__construct()
{
$ this-> exploits = new ArrayCollection();
}

/ **
*设置名称
*
* @参数字符串$ name
*
* @return作者
* /
公共功能setName($ name)
{
$ this-> name = $ name;

返回$ this;
}

/ **
*获取名称
*
* @返回字符串
* /
公共函数getName( )
{
返回$ this->名称;
}

/ **
*获取ID
*
* /
公共函数getId()
{
返回$ this-> id;
}

公共函数__toString()
{
return $ this-> name;
}


/ **
* @param Exploits $ exploit
*
* @return author
* /
公共功能addExploit($ exploit)
{
$ this-> exploits-> add($ exploit);

返回$ this;
}

/ **
* @param Collection $ exploits
*
* @return作者
* /
public函数setExploits(Collection $ exploits)
{
$ this-> exploits-> clear();

foreach($ exploits as $ exploit){
$ exploit-> add($ this);
}

$ this-> exploits = $ exploits;

返回$ this;
}
}
:::::::::::::::
Category.php
::::::::::: :::
<?php
命名空间AppBundle\Entity;

使用Doctrine\ORM\Mapping作为ORM;
使用AppBundle\Entity\Exploit;

/ **
*类别
*
* @ ORM\Table(name = category,index = {@ ORM\Index(name = category_name_id_idx,columns = { id})})
* @ ORM\实体
* /
类类别
{

/ **
* @ ORM\Id
* @ ORM\GeneratedValue(strategy = NONE)
* /
private $ id;

/ **
* @变量字符串
*
* @ ORM\Column(name = name,category = string,length = 255 ,nullable = false)
* /
私人$ name;


/ **
* @ ORM\OneToMany(targetEntity = AppBundle\Entity\Exploit,mappingBy = category,级联= { persist , remove})
* @ ORM\JoinColumn(name = exploits,referencedColumnName = id)
* /
private $ exploits;

/ **
*作者构造函数。
* /
公共函数__construct()
{
$ this-> exploits = new ArrayCollection();
}

/ **
*设置名称
*
* @参数字符串$ name
*
* @return作者
* /
公共功能setName($ name)
{
$ this-> name = $ name;

返回$ this;
}

/ **
*获取名称
*
* @返回字符串
* /
公共函数getName( )
{
return $ this-> name;
}

/ **
*获取ID
*
* /
公共函数getId()
{
返回$ this-> id;
}

公共函数__toString()
{
return $ this-> name;
}


/ **
* @param漏洞利用$ exploit
*
* @返回作者
* /
公共功能addExploit($ exploit)
{
$ this-> exploits-> add($ exploit);

返回$ this;
}

/ **
* @param Collection $ exploits
*
* @return作者
* /
public函数setExploits(Collection $ exploits)
{
$ this-> exploits-> clear();

foreach($ exploits as $ exploit){
$ exploit-> add($ this);
}

$ this-> exploits = $ exploits;

返回$ this;
}
}
:::::::::::::::
Exploit.php
::::::::::: :::
<?php

命名空间AppBundle\Entity;

使用Doctrine\ORM\Mapping作为ORM;
使用AppBundle\Entity\Author;
使用AppBundle\Entity\Type;
使用AppBundle\Entity\Category;

/ **
*利用
*
* @ ORM\Table(name = exploit,index = {@ ORM\Index(name = exploit_category_idx,列= { category}),@ ORM\Index(name = exploit_type_idx,列= { type}),@ ORM\Index(name = exploit_author_idx,列= {
author})})
* /
class利用
{
/ **
* @var整数
*
* @ ORM\Column(name = id,type = bigint,nullable = false)
* @ ORM\Id
* @ ORM\GeneratedValue(strategy = IDENTITY)
* /
private $ id;

/ **
* @var字符串
*
* @ ORM\Column(name = edb_id,type = string,长度= 100,可为空= 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;

/ **
* @变量字符串
*
* @ ORM\Column(name = name,type = string,length = 255 ,nullable = false)
* /
私人$ name;

/ **
* @var整数
*
* @ ORM\ManyToOne(targetEntity = AppBundle\Entity\Category,inversedBy = exploits)
* @ ORM\JoinColumn(name = category,referencedColumnName = id)
* /
private $ category;

/ **
* @变量字符串
*
* @ ORM\Column(name = version,type = string,length = 255 ,nullable = false)
* /
private $ version;

/ **
* @var整数
*
* @ ORM\ManyToOne(targetEntity = AppBundle\Entity\Type,inversedBy = exploits)
* @ ORM\JoinColumn(name = type,referencedColumnName = id)
* /
private $ type;

/ **
* @变量字符串
*
* @ ORM\Column(name = content,type = text,nullable = false )
* /
私人$ content;

/ **
* @变量字符串
*
* @ ORM\Column(name = dork,type = string,length = 255 ,nullable = true)
* /
私人$ dork;

/ **
* @var字符串
*
* @ ORM\Column(name = software_link,type = string,length = 255 ,nullable = true)
* /
private $ softwareLink;

/ **
* @变量字符串
*
* @ ORM\Column(name = tested_on,type = string,length = 255 ,nullable = true)
* /
private $ testedOn;



/ **
*设置edbId
*
* @param整数$ edbId
*
* @return Exploit
* /
公共功能setEdbId($ edbId)
{
$ this-> edbId = $ edbId;

返回$ this;
}

/ **
*获取edbId
*
* @返回整数
* /
公共函数getEdbId( )
{
return $ this-> edbId;
}

/ **
*设置日期
*
* @param \DateTime $ date
*
* @return Exploit
* /
公共功能setDate($ date)
{
$ this-> date = $ date;

返回$ this;
}

/ **
*获取日期
*
* @return \DateTime
* /
公共功能getDate()
{
return $ this-> date;
}

/ **
*设置作者
*
* @参数整数$ author
*
* @return利用
* /
公共功能setAuthor($ author)
{
$ this-> author = $ author;

返回$ this;
}

/ **
*获取作者
*
* @返回整数
* /
公共函数getAuthor( )
{
return $ this-> author;
}

/ **
*设置名称
*
* @参数字符串$ name
*
* @return利用
* /
公共功能setName($ name)
{
$ this-> name = $ name;

返回$ this;
}

/ **
*获取名称
*
* @返回字符串
* /
公共函数getName( )
{
返回$ this->名称;
}

/ **
*设置类别
*
* @参数整数$ category
*
* @return利用
* /
公共功能setCategory($ category)
{
$ this-> category = $ category;

返回$ this;
}

/ **
*获取类别
*
* @返回整数
* /
公共函数getCategory( )
{
return $ this->类别;
}

/ **
*设置版本
*
* @param string $ version
*
* @return利用
* /
公共功能setVersion($ version)
{
$ this-&version; $ version;

返回$ this;
}

/ **
*获取版本
*
* @返回字符串
* /
public function getVersion( )
{
返回$ this->版本;
}

/ **
*设置类型
*
* @参数整数$ type
*
* @return利用
* /
公共功能setType($ type)
{
$ this-> type = $ type;

返回$ this;
}

/ **
*获取类型
*
* @返回整数
* /
public function getType( )
{
return $ this-> type;
}

/ **
*设置内容
*
* @param string $ content
*
* @return利用
* /
公共功能setContent($ content)
{
$ this-> content = $ content;

返回$ this;
}

/ **
*获取内容
*
* @返回字符串
* /
公共函数getContent( )
{
返回$ this->内容;
}

/ **
*设置dork
*
* @param string $ dork
*
* @return利用
* /
公共功能setDork($ dork)
{
$ this-> dork = $ dork;

返回$ this;
}

/ **
*获取dork
*
* @返回字符串
* /
公共函数getDork( )
{
return $ this-> dork;
}

/ **
*设置softwareLink
*
* @param string $ softwareLink
*
* @return利用
* /
公共功能setSoftwareLink($ softwareLink)
{
$ this-> softwareLink = $ softwareLink;

返回$ this;
}

/ **
*获取softwareLink
*
* @返回字符串
* /
公共函数getSoftwareLink( )
{
return $ this-> softwareLink;
}

/ **
*设置testsOn
*
* @param字符串$ testedOn
*
* @return利用
* /
公共功能setTestedOn($ testedOn)
{
$ this-&testedOn = $ testedOn;

返回$ this;
}

/ **
*得到测试On
*
* @返回字符串
* /
公共函数getTestedOn( )
{
return $ this-> testedOn;
}

/ **
*获取ID
*
* @返回整数
* /
公共函数getId( )
{
返回$ this-> id;
}
}
:::::::::::::::
Type.php
::::::::::: :::
<?php
命名空间AppBundle\Entity;

使用Doctrine\ORM\Mapping作为ORM;
使用AppBundle\Entity\Exploit;

/ **
*类型
*
* @ ORM\Table(name = type,index = {@ ORM\Index(name = type_name_id_idx,columns = { id})})
* @ ORM\实体
* /
类类型
{

/ **
* @ ORM\Id
* @ ORM\GeneratedValue(strategy = NONE)
* /
private $ id;

/ **
* @变量字符串
*
* @ ORM\Column(name = name,type = string,length = 255 ,nullable = false)
* /
私人$ name;


/ **
* @ ORM\OneToMany(targetEntity = AppBundle\Entity\Exploit,mappingBy = type,级联= { persist , remove})
* @ ORM\JoinColumn(name = exploits,referencedColumnName = id)
* /
private $ exploits;

/ **
*类型构造函数。
* /
公共函数__construct()
{
$ this-> exploits = new ArrayCollection();
}

/ **
*设置名称
*
* @参数字符串$ name
*
* @return输入
* /
公共功能setName($ name)
{
$ this-> name = $ name;

返回$ this;
}

/ **
*获取名称
*
* @返回字符串
* /
公共函数getName( )
{
返回$ this->名称;
}

/ **
*获取ID
*
* /
公共函数getId()
{
返回$ this-> id;
}

公共函数__toString()
{
return $ this-> name;
}


/ **
* @param漏洞利用$ exploit
*
* @返回类型
* /
公共功能addExploit($ exploit)
{
$ this-> exploits-> add($ exploit);

返回$ this;
}

/ **
* @param Collection $ exploits
*
* @返回类型
* /
public函数setExploits(Collection $ exploits)
{
$ this-> exploits-> clear();

foreach($ exploits as $ exploit){
$ exploit-> add($ this);
}

$ this-> exploits = $ exploits;

返回$ this;
}

}

但是我查询时不知何故我得到像这样的

  $ exploits = $ this-> getDoctrine()
-> getRepository(' AppBundle:Exploit')
-> findAll();

在视野中

 < 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>

我收到此错误:

 无法访问整数变量( 1)上的属性(名称)。 

任何善良的灵魂都可以研究并尝试重现吗?



MySQL转储以重新创建包含内容的表在这里:



https://0bin.net/paste/2tV3MEw4A2tdAVsR#R3rBNW4seWkK9HtlJFwbsA6+T$p >

它说的正确,您正在尝试访问 name 属性的东西,但那不是一个对象。它是 1



您可以访问 .name 几个如此不确定是否是其中之一而没有更多信息和行号

 < 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>

但是如果我们假设其 Author 清除 getAuthor()方法将返回 bigint



您是如何获得实体代码和数据库代码的?因为它们看起来不正确,所以它们正在获取并设置整数,而不是对象。



UPDATE



生成具有

  php bin / console的实体:generate:entities AppBundle 

使用以下命令查看SQL

  php bin /控制台学说:schema:update --dump-sql 

使用以下命令执行SQL >

  php bin /控制台学说:schema:update --force 


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

这篇关于Symfony3-实体不适用于关系(具有索引和约束的数据库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆