如何使用另一个存储的id获取特定的实体属性?

问题描述 投票:0回答:2

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

学说很多很多人或其他协会会有帮助吗?

/**
 * 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
2个回答
0
投票

这个解决方案怎么样:

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>

0
投票

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

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

public function __construct(){
    $this->annonceemplois = new ArrayCollection();    
}

/**
 * @ORM\OneToMany(targetEntity="App\Entity\Annonceemplois", 
 mappedBy="postulation")
 */
    private $annonceemplois;

在Ademplois.php

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\Postulation", 
 inversedBy="annoceemplois")
 */
    private $postulation;
© www.soinside.com 2019 - 2024. All rights reserved.