【问题标题】:Doctrine 2 ManyToOne mapping annotationsDoctrine 2 多对一映射注释
【发布时间】:2012-03-16 14:37:49
【问题描述】:

TwitterTweets 实体:

/**
 * MyBundle\CoreBundle\Entity\TwitterTweets
 *
 * @ORM\Table(name="twitter_tweets")
 * @ORM\Entity
 */
class TwitterTweets
{
    /**
     * @var TwitterUsers
     *
     * @ORM\ManyToOne(targetEntity="TwitterUsers", inversedBy="tweets")
     * @ORM\JoinTable(name="twitter_tweets",
     *   joinColumns={
     *     @ORM\JoinColumn(name="twitter_user_id", referencedColumnName="twitter_id")
     *   }
     * )
     */
    private $twitterUser;
}

TwitterUsers 实体:

/**
 * MyBundle\CoreBundle\Entity\TwitterUsers
 *
 * @ORM\Table(name="twitter_users")
 * @ORM\Entity
 */
class TwitterUsers
{
    /**
     * @var TwitterTweets
     *
     * @ORM\OneToMany(targetEntity="TwitterTweets", mappedBy="twitterUser")
     */
    private $tweets;
}

twitter_tweets 表:

CREATE TABLE `twitter_tweets` (
 `period` int(6) unsigned NOT NULL,
 `tweet_id` varchar(30) NOT NULL,
 `twitter_user_id` bigint(20) unsigned NOT NULL,
 `tweet` varchar(255) NOT NULL,
 `url` text NOT NULL,
 `retweet_count` varchar(10) NOT NULL DEFAULT '0',
 `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
 PRIMARY KEY (`period`,`tweet_id`),
 KEY `period` (`period`),
 KEY `tweet_id` (`tweet_id`),
 KEY `twitter_user_id` (`twitter_user_id`),
 KEY `created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

twitter_users 表:

CREATE TABLE `twitter_users` (
 `twitter_id` bigint(20) unsigned NOT NULL,
 `user` varchar(255) NOT NULL,
 `username` varchar(255) NOT NULL,
 `profile_image_url` text NOT NULL,
 PRIMARY KEY (`twitter_id`),
 KEY `user` (`user`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

执行简单的 SELECT 时出现此错误:

$this->getDoctrine()->getRepository('MyBundleCoreBundle:TwitterTweets')->findOneBy(array( 'tweetId' => $data->tweet_id ))

找不到列:1054 '字段列表'中的未知列't0.twitterUser_id'

SELECT t0.period AS period1, t0.tweet_id AS tweet_id2, t0.tweet AS tweet3,
t0.url AS url4, t0.retweet_count AS retweet_count5, t0.created_at AS created_at6,
t0.twitterUser_id AS twitterUser_id7
FROM twitter_tweets t0 WHERE t0.tweet_id = ?

我该如何解决这个问题? 我试图只设置 @ORM\JoinColumn (没有 JoinTable 注释),但我得到了这个错误:

"message":"SQLSTATE[42S02]: 未找到基表或视图:1146 表 'database.twittertweets_twittertrends' 不存在"

【问题讨论】:

    标签: php symfony doctrine-orm mysql-error-1146


    【解决方案1】:

    使用多对一单向关联映射解决:http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/association-mapping.html#many-to-one-unidirectional

    TwitterTweets 实体:

    /**
     * MyBundle\CoreBundle\Entity\TwitterTweets
     *
     * @ORM\Table(name="twitter_tweets")
     * @ORM\Entity
     */
    class TwitterTweets
    {
        /**
         * @var TwitterUsers
         *
         * @ORM\ManyToOne(targetEntity="TwitterUsers")
         * @ORM\JoinColumn(name="twitter_user_id", referencedColumnName="twitter_id")
         */
        private $twitterUser;
    }
    

    TwitterUsers 实体:

    /**
     * MyBundle\CoreBundle\Entity\TwitterUsers
     *
     * @ORM\Table(name="twitter_users")
     * @ORM\Entity
     */
    class TwitterUsers
    {
        // ... no properties needed
    }
    

    感谢 jperovic 的帮助 :)

    【讨论】:

      【解决方案2】:

      实际上Many-To-Many 是通过@JoinTable 定义的。要定义Many-To-One,您只需执行以下操作:

      /**
           * @var TwitterUsers
           *
           * @ORM\ManyToOne(targetEntity="TwitterUsers", inversedBy="tweets")
           * @ORM\JoinColumn(name="twitter_user_id", referencedColumnName="twitter_id")
           */
      

      【讨论】:

      • 感谢您的帮助,但正如我之前所写,我尝试仅设置 @ORM\JoinColumn (与您的示例完全相同)但它不起作用:“message”: “SQLSTATE[42S02]:未找到基表或视图:1146 表 'database.twittertweets_twittertrends' 不存在”
      • 好吧,我不知道我是盲人还是什么,但我完全错过了你写的关于@JoinColumn的问题的最后一部分。对于那个很抱歉。您可以使用放在实体顶部的@Table 注释来更新您的问题吗?我怀疑你要么拼错了表名,要么没有放注释根本...
      • 我现在在我的实体顶部添加了表格注释。我认为他们是正确的。提前致谢!
      • 是的,这些看起来是正确的。我看不到 _twittertrends 表名后缀的来源。虽然这两个实体属于同一个命名空间,但您是否有可能拥有其他同名实体(显然来自其他命名空间)?
      猜你喜欢
      • 2015-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 2014-06-02
      相关资源
      最近更新 更多