微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用路由中的参数通过元数据访问保存的实体

如何解决如何使用路由中的参数通过元数据访问保存的实体

我有实体联系人

<?PHP

namespace App\Entity;

use App\Repository\ContactRepository;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=ContactRepository::class)
 */
class Contact
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string",length=50)
     */
    private $firstName;

    /**
     * @ORM\Column(type="string",length=50)
     */
    private $lastName;

    /**
     * @ORM\Column(type="date",nullable=true)
     */
    private $dob;

    /**
     * @ORM\Column(type="string",length=50)
     */
    private $email;

    
    /**
     * @ORM\Column(type="string",length=15)
     */
    private $phone;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getFirstName(): ?string
    {
        return $this->firstName;
    }

    public function setFirstName(string $firstName): self
    {
        $this->firstName = $firstName;

        return $this;
    }

    public function getLastName(): ?string
    {
        return $this->lastName;
    }

    public function setLastName(string $lastName): self
    {
        $this->lastName = $lastName;

        return $this;
    }

    public function getdob(): ?\DateTimeInterface
    {
        return $this->dob;
    }

    public function setdob(?\DateTimeInterface $dob): self
    {
        $this->dob = $dob;

        return $this;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    public function getPhone(): ?string
    {
        return $this->phone;
    }

    public function setPhone(string $phone): self
    {
        $this->phone = $phone;

        return $this;
    }
}

我想使用以下URL选择一个(或多个)联系人:/c00210/readallbyparam/{routeProperty}/{routeValue}

例如:/c00210/readallbyparam/lastname/Mendelsohn
这应该返回2条记录:

  • Felix Mendelsohn
  • 范妮·门德尔松

如何编写render()函数? 如何编写树枝指令?

我想我应该使用元数据,但是我不知道

++++++++++++++++++++++++++++++++++++++++

这是我控制器中的功能

/**
 * @Route("/c00210/readallbyparam/{routeProperty}/{routeValue}",name="c00210_read_all_by_param")
 */

public function findAllByparam( $routeProperty,$routeValue)
{

    $property = $routeProperty;
    $value = $routeValue;
    

    $repository = $this->getDoctrine()->getRepository(Contact::class);
    $contacts = $repository->findAll();

    return $this->render('c00210/readbycriteria2.html.twig',[
        'function' => 'find(<id>)','description' => 'finds the with the given Id','property' => $property,'value' => $value,'contacts' => $contacts,]);

routeProperty应该是列名,而routeValue应该是列值

应该等同于

SELECT * FROM contacts WHERE lastname = 'Mendelsohn'

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。