简体   繁体   中英

Symfony3 I have to show from a related entity

I have a question.

I have 2 entity

/**
 * @ORM\Entity
 * @ORM\Table(name="call_center")
 */
class Call {

    /**
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Id
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\OneToMany(targetEntity="Number", mappedBy="number")
     * @ORM\Column(type="string")
     */
    private $number;

    /**
     * @ORM\Column(type="string")
     */
      private $value;
......
    getters setters

/**
 * @ORM\Entity
 * @ORM\Table(name="number")
 */
class Number {

    /**
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Id
     * @ORM\Column(type="integer")
     * 
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Call", inversedBy="number")
     * @ORM\JoinColumn(nullable=false)
     */
    private $number;

    /**
     * @ORM\Column(type="string")
     */
    private $link;

And I would like to show my data in controller. This is my controller

class DefaultController extends Controller
{
    /**
     * @Route("/pl/", name="homepage")
     */
    public function indexAction(Request $request)
    {
        $em = $this->getDoctrine()->getRepository('AppBundle:Call')->findAll();

        foreach ($em as $name) {
        switch(1) {
            case $name->getNumber():
                echo $name->getValue();
                echo $name->getLink(); <----PROBLEME
                break;
            default:
                break;
        }
        }

        return $this->render('default/index.html.twig', array(
            'em' => $name
        ));
    }
}

Data with entity call displayed but I don't know how dipsplay data from Number (getLink()). The problem is that I have a loop in which I have to display for a particular value relationship. Probably I have to create repository for entity?

Entity Call
/**
 * Get id
 *
 * @return integer
 */
public function getId()
{
    return $this->id;
}

/**
 * Set number
 *
 * @param string $number
 *
 * @return Call
 */
public function setNumber($number)
{
    $this->number = $number;

    return $this;
}

/**
 * Get number
 *
 * @return string
 */
public function getNumber()
{
    return $this->number;
}

/**
 * Set value
 *
 * @param string $value
 *
 * @return Call
 */
public function setValue($value)
{
    $this->value = $value;

    return $this;
}

/**
 * Get value
 *
 * @return string
 */
public function getValue()
{
    return $this->value;
}

entity Number

/**
     * Set number
     *
     * @param \AppBundle\Entity\Call $number
     *
     * @return Number
     */
    public function setNumber(\AppBundle\Entity\Call $number)
    {
        $this->number = $number;

        return $this;
    }

    /**
     * Get number
     *
     * @return \AppBundle\Entity\Call
     */
    public function getNumber()
    {
        return $this->number;
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set link
     *
     * @param string $link
     *
     * @return Number
     */
    public function setLink($link)
    {
        $this->link = $link;

        return $this;
    }

    /**
     * Get link
     *
     * @return string
     */
    public function getLink()
    {
        return $this->link;
    }

Have you tried to access it via $name->getNumber()->getLink() ?

as pointed out below, by tny, you should fix your annotations, or the above will not work, as getNumber() is currently returning a string instead of a Number instance

Maybe you have a string because you tell doctrine to put a string ?

remove the following annotation from your relation :

@ORM\Column(type="string")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM