gpt4 book ai didi

mysql - Symfony2/ Doctrine 关系映射

转载 作者:行者123 更新时间:2023-11-29 13:54:03 25 4
gpt4 key购买 nike

我正在使用 syfmony2 框架构建我的第一个应用程序。
现在我坚持一点关于如何从我的数据库中获取数据。

首先是我的表格(仅重要字段)

# 客户

客户 ID |情报 |人工智能 | PK

# 文章

article_id |情报 |人工智能 | PK

号码 | VARCHAR

# 客户文章 (两个领域都建立了PK)

客户 ID |情报 | PK

article_id |情报 | PK

现在我想执行一个简单的查询,如下所示:

SELECT 
ca.article_id, a.number
FROM
customer_article AS ca
LEFT JOIN
article AS a ON a.article_id = ca.article_id
WHERE
ca.customer_id = {customer_id}

据我了解文档最好的方法是
在我的实体 php 文件中进行关系映射。

客户.php

文章.php

CustomerArticle.php

但我很困惑该做什么以及朝哪个方向。
( http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html)

在我看来,这一定是正确的关系

客户到客户文章是一对多(单向)关系

CustomerArticle 到 Customer 是多对一(单向)关系

文章到客户文章是一对多(单向)关系

CustomerArticle to Article 是多对一(单向)关系

在这一点上是对的吗?

现在是否有必要在每个
实体文件?还是我只需要以一种方式进行,例如
客户对客户文章(一对多 - 单向)和
文章到客户文章(一对多 - 单向)

不幸的是,“一到五月,单向”是唯一一个没有
Doctrine 引用页面上的适当示例。

他们写:
“从 Doctrine 的角度来看,它被简单地映射为单向多对多,其中一个连接列上的唯一约束强制执行一对多基数”

这是否意味着我只需要像页面的“单向多对多”部分那样进行映射?

很抱歉有很多问题。

我在文档中阅读了很多内容,但我无法自己回答这些问题。

谢谢你的帮助。

2013 年 4 月 26 日更新

不幸的是,即使在 Lighthart 的帮助和阅读 Doctrine 文档的帮助下,我也无法让它工作。

我认为向您提供有关我的表格和代码的更多详细信息会很有帮助。也许有人可以给我基本的答案,所以我最终理解了它背后的概念。

我的表格和一些示例条目
Table: customer (PK: customer_id)
+-------------+--------+----------+-------------------+------+-----------+
| customer_id | number | password | email | salt | is_active |
+-------------+--------+----------+-------------------+------+-----------+
| 1 | CU0001 | 43t9wef | test@example.com | test | 1 |
| 2 | CU0002 | 32rk329 | test2@example.com | test | 1 |
+-------------+--------+----------+-------------------+------+-----------+

Table: article (PK: article_id)
+-------------+---------+
| article_id | number |
+-------------+---------+
| 1 | TEST-01 |
| 2 | TEST-02 |
| 3 | TEST-03 |
| 4 | TEST-04 |
| 5 | TEST-05 |
+-------------+---------+

自从我第一次发帖以来我做了什么?
  • 删除 CustomerArticle.php
  • 删除 customer_article 表
  • 向客户(和文章)实体添加多对多关系

  • 这是实体文件此时的样子(没有方法)

    客户.php
    <?php
    namespace Test\ExampleBundle\Entity;

    use Symfony\Component\Security\Core\User\UserInterface;
    use Doctrine\ORM\Mapping as ORM;

    /**
    * @ORM\Entity
    * @ORM\Table(name="customer")
    */
    class Customer implements UserInterface
    {
    /**
    * @ORM\Id
    * @ORM\Column(type="integer")
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    protected $customer_id;

    /**
    * @ORM\ManyToMany(targetEntity="Article", inversedBy="customers")
    * @ORM\JoinTable(name="customer_article",
    * joinColumns={ @ORM\JoinColumn(name="customer_id", referencedColumnName="customer_id" ) }
    * )
    */
    private $articles;

    /**
    * @ORM\Column(type="string", length=255, nullable=false, unique=true)
    */
    protected $number;

    /**
    * @ORM\Column(type="string", length=255, nullable=false)
    */
    protected $password;

    /**
    * @ORM\Column(type="string", length=255, nullable=true)
    */
    protected $email;

    /**
    * @ORM\Column(type="string", length=255)
    */
    protected $salt;

    /**
    * @ORM\Column(name="is_active", type="boolean")
    *
    */
    protected $isActive;

    // ... methods
    }

    文章.php
    <?php
    namespace Test\ExampleBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;

    /**
    * @ORM\Entity
    * @ORM\Table(name="article")
    */
    class Article
    {
    /**
    * @ORM\Id
    * @ORM\Column(type="integer")
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    protected $article_id;

    /**
    * @ORM\ManyToMany(targetEntity="Customer", mappedBy="articles")
    */
    private $customers;

    /**
    * @ORM\Column(type="string", length=255, nullable=false, unique=true)
    */
    protected $number;

    // ... methods
    }

    当我尝试执行时:
    php app/console doctrine:schema:update --force

    我收到以下消息
    [Doctrine\ORM\ORMException]
    Column name `id` referenced for relation from Test\ExampleBundleBundle\Entity\Customer towards Test\ExampleBundleBundle\Entity\Article does not exist.

    我不知道为什么 Doctrine 要使用列名“id”。我阅读了有关此错误的内容并添加了“JoinColumns”-Code,但没有帮助。

    我的目标是什么?

    我将一些 csv 文件中的数据导入表中:
    “customer”、“article” 以及当前不存在的表“customer_article”。

    在我的应用程序中,每个客户都有几篇引用他的文章。
    所以表格看起来像这样
    customer_article
    +-------------+------------+
    | customer_id | article_id |
    +-------------+------------+
    | 1 | 1 |
    | 1 | 2 |
    | 1 | 5 |
    | 2 | 2 |
    | 2 | 5 |
    +-------------+------------+

    这不是商店或类似的东西。这很难解释,所以首先只需要知道每个客户都有未定义数量的相关文章。

    现在我必须能够使用 tthe customer_id 获取与一位客户相关的所有文章。 (看我上面开始帖子中的示例 sql 语句)

    我希望你能帮助我。

    非常感谢大家。

    最佳答案

    这看起来真的像客户和文章的双向多对多。您将构建的实体是 Customer.php 和 Article.php,您可以通过注释在这些文件中指定多对多,或者根据您的偏好在 Doctrine .orm.yml 中指定。

    Doctrine 将为您制作中间表(customer_article)并进行管理。

    您将使用 DQL:

    $query = $em->createQuery("SELECT a FROM Article a JOIN a.customer c");
    $users = $query->getResult();

    然后,您将文章作为实体处理,并调用 article.getCustomer() 以获取与文章关联的客户列表。

    这是一个与 SQL 完全不同的范例,需要一些时间来适应。

    关于mysql - Symfony2/ Doctrine 关系映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16170566/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com