【问题标题】:How to get a specific entity attribute using the id stored on another one?如何使用存储在另一个实体上的 id 获取特定实体属性?
【发布时间】:2019-04-15 19:22:44
【问题描述】:

所以我有一个假设实体,其中包含另一个名为 annoncesemplois 的 id,我想做的是使用存储在实体假设中的 idAnnEmp 并从数据库中获取 annoncesemplois 的标题(tire),并使用显示树枝

原则多对多或其他关联有帮助吗?

/**
 * Annonceemplois
 *
 * @ORM\Table(name="annonceemplois", indexes={@ORM\Index(name="FK_ANNONCEEMPLOIS_idUser", columns={"idUser"})})
 * @ORM\Entity
 */
class Annonceemplois
{
    /**
     * @var integer
     *
     * @ORM\Column(name="IDANNEMP", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $idannemp;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="DATECREATION", type="date", nullable=true)
     */
    private $datecreation;

    /**
     * @var float
     *
     * @ORM\Column(name="PRIX", type="float", precision=10, scale=0, nullable=true)
     */
    private $prix;

    /**
     * @var string
     *
     * @ORM\Column(name="TITRE", type="string", length=255, nullable=true)
     */
    private $titre;

    /**
     * @var string
     *
     * @ORM\Column(name="DESCRIPTION", type="string", length=255, nullable=true)
     */
    private $description;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="DUREE", type="date", nullable=true)
     */
    private $duree;

    /**
     * @var integer
     *
     * @ORM\Column(name="idUser", type="integer", nullable=true)
     * 
     */
    private $iduser;
/**
 * Postulation
 *
 * @ORM\Table(name="postulation", indexes={@ORM\Index(name="FK_POSTULATION_id", columns={"id"}), @ORM\Index(name="FK_POSTULATION_idAnnEmp", columns={"idAnnEmp"})})
 * @ORM\Entity
 */
class Postulation
{
    /**
     * @var integer
     *
     * @ORM\Column(name="IDPOSTULATION", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $idpostulation;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="DATEPOSTULATION", type="date", nullable=true)
     */
    private $datepostulation;

    /**
     * @var string
     *
     * @ORM\Column(name="ETAT", type="string", length=255, nullable=true)
     */
    private $etat;

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=true)
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\Column(name="idAnnEmp", type="integer", nullable=true)
     */
    private $idannemp;

这是我试图展示它的地方

<div class="row">
                <div class="col-md-9">

                    <div class="panel panel-default">
                        <div class="panel-body posts">

                            <div class="row">
                                <div class="col-md-6">
                                    {% for post in postulations %}
                                    <div class="post-item">
                                        <div class="post-title">
                                           {{ post.idannemp.titre }}
                                        </div>
                                        <div class="post-date"><span class="fa fa-calendar"></span> {{ post.datepostulation | date }} / {{ post.etat }}</div>
                                    </div>
                                    {% endfor %}
                                </div>

                            </div>

                        </div>
                    </div>
                </div>
            </div>

【问题讨论】:

    标签: php symfony doctrine


    【解决方案1】:

    这个解决方案怎么样:

    class Annonce 
    {
        /**
         * @ORM\OneToMany(targetEntity="Postulation", mappedBy="annonce")
         *
         */
        private $postulations;
    
        public function __construct()
        {
            $this->postulations = new ArrayCollection();
        }
    }
    
    class Postulation
    {
       /**
        *@ORM\ManyToOne(targetEntity="Annonce", inversedBy="postulations")
        *@ORM\JoinColumn(name="annonce_id", referencedColumnName="id")
        */
        private $annonce;
    }
    

    在树枝中你可以使用这个:

    <div class="row">
                    <div class="col-md-9">
    
                        <div class="panel panel-default">
                            <div class="panel-body posts">
    
                                <div class="row">
                                    <div class="col-md-6">
                                        {% for post in postulations %}
                                        <div class="post-item">
                                            <div class="post-title">
                                               {{ post.annonce.titre }}
                                            </div>
                                            <div class="post-date"><span class="fa fa-calendar"></span> {{ post.datepostulation | date }} / {{ post.etat }}</div>
                                        </div>
                                        {% endfor %}
                                    </div>
    
                                </div>
    
                            </div>
                        </div>
                    </div>
                </div>
    

    【讨论】:

    • 现在好像可以了,但它说它是一个空变量,我在这里缺少什么?
    • 无法访问空变量的属性(“滴度”)。
    • 逻辑上不应该有没有广告的假设。我们可以为@ORM\JoinColumn(name="annonce_id", referencedColumnName="id")添加nullable = false
    【解决方案2】:

    您可以使用关系来获取相关实体。例如:

    在 Postulations.php 中添加 oneToMany 关系并初始化字段:

    public function __construct(){
        $this->annonceemplois = new ArrayCollection();    
    }
    
    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Annonceemplois", 
     mappedBy="postulation")
     */
        private $annonceemplois;
    

    在Annonceemplois.php中

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Postulation", 
     inversedBy="annoceemplois")
     */
        private $postulation;
    

    【讨论】:

    • 我这样做了,但是当我输入 post.annonceemplois.Titre 时给出错误滴度,getTitre 不存在
    • 属性 "Titre" 和方法 "Titre()"、"getTitre()"/"isTitre()" 或 "__call()" 都不存在并且在类 "学说\ORM\PersistentCollection”。仍然是这个错误,我是新手,我几乎不知道该怎么做。
    • 你插入数据和数据库的映射是否正确?
    • ` $em = $this->getDoctrine()->getManager(); $postulations = $em->getRepository('SmartStartsBundle:Postulation')->findAll(); $annonceemplois = $em->getRepository('SmartStartsBundle:Annonceemplois')->findAll(); foreach ($postulations as $post){ foreach ($annonceemplois as $an){ if ($post->getIdannemp()===$an->getIdannemp()){ $post->setAnnonce($an); } } } return $this->render('postulation/index.html.twig', array( 'postulations' =>$postulations, )); `
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-20
    • 2014-03-29
    • 1970-01-01
    • 2014-10-09
    • 1970-01-01
    相关资源
    最近更新 更多