标签: doctrine-orm

doctrine2 dql,在进行类似比较时使用带有%wildcard的setParameter

我想使用参数占位符 - 例如?1 - 与%外卡.也就是说,"u.name LIKE%?1%"(虽然这会引发错误).文档有以下两个例子:1.

// Example - $qb->expr()->like('u.firstname', $qb->expr()->literal('Gui%'))
public function like($x, $y); // Returns Expr\Comparison instance
Run Code Online (Sandbox Code Playgroud)

我不喜欢这样,因为没有针对代码注入的保护.

2.

// $qb instanceof QueryBuilder

// example8: QueryBuilder port of: "SELECT u FROM User u WHERE u.id = ?1 OR u.nickname LIKE ?2 ORDER BY u.surname DESC" using QueryBuilder helper methods
$qb->select(array('u')) // string 'u' is converted to array internally
   ->from('User', 'u')
   ->where($qb->expr()->orx(
       $qb->expr()->eq('u.id', '?1'),
       $qb->expr()->like('u.nickname', '?2')
   ))
   ->orderBy('u.surname', 'ASC'));
Run Code Online (Sandbox Code Playgroud)

我不喜欢这个,因为我需要在对象的属性中搜索术语 - 也就是说,我需要两侧的外卡.

wildcard code-injection dql doctrine-orm sql-like

28
推荐指数
2
解决办法
3万
查看次数

如何检查Doctrine 2中是否更改了实体?

我需要检查持久化实体是否已更改并需要在数据库上更新.我所做的(并没有奏效)如下:

$product = $entityManager->getRepository('Product')->find(3);
$product->setName('A different name');

var_export($entityManager->getUnitOfWork()->isScheduledForUpdate($product));
Run Code Online (Sandbox Code Playgroud)

该代码始终打印false,我也在检查工作单元之前尝试刷新,但没有工作.

有人有建议吗?

php doctrine doctrine-orm

28
推荐指数
4
解决办法
3万
查看次数

期望一个或者没有来自学说查询构建器的结果,我应该使用什么?

我有这个方法:

public function getMonth ($month_name)
    {
        $q = $this->createQueryBuilder('m');

        $q->select('m')
            ->where('m.name = :name')    
            ->setParameter('name', $month_name);

        return $q->getQuery()->getResult();
    }
Run Code Online (Sandbox Code Playgroud)

从中我希望找到一个月或0个月.我在控制器中以这种方式使用此方法:

$month = $em->getRepository('EMExpensesBundle:Month')
                ->getMonth($this->findMonth());

            $month->setSpended($item->getPrice());
Run Code Online (Sandbox Code Playgroud)

我尝试了这getSingleResult()一切,一切都很完美,直到我遇到一个没有找到月份的情况,一切都失败了!

然后我尝试了getResult(),但它返回一个数组,然后

$month->setSpended($item->getPrice());
Run Code Online (Sandbox Code Playgroud)

据说被称为非对象并修复它我应该到处使用

$month[0]->setSpended($item->getPrice());
Run Code Online (Sandbox Code Playgroud)

是否有更优雅的方法来实现这一点,而无需在任何地方添加不必要的[0]索引?

php symfony doctrine-orm

28
推荐指数
2
解决办法
3万
查看次数

Doctrine架构更新或Doctrine迁移

仅仅运行架构更新,Doctrine Migrations有哪些实际优势?

安全?

orm:schema-tool:update命令(doctrine:schema:update在Symfony中)发出警告

不应在生产环境中执行此操作.

但为什么会这样呢?当然,它可以删除数据,但迁移也是如此.

灵活性?

我认为我可以定制我的迁移以添加列默认值之类的东西,但这通常不起作用,因为Doctrine会注意到架构和下一个差异上的代码之间的差异并踩踏你的更改.

database-schema symfony doctrine-orm

28
推荐指数
1
解决办法
8731
查看次数

Symfony 2还是Symfony 1.4?

我正在开始一个对我的公司非常重要的新Symfony项目.我的经验仅适用于Symfony 1.4.我有3个月的时间来完成这个项目.

该项目应该存在多年,并将成长为具有许多功能.我知道很多人已经在生产中使用Symfony 2,但你认为使用1.4是个不错的主意吗?

每种情况都不同.我没有看到1.4的任何问题,但有些人建议我使用Symfony 2,因为最终我们需要升级并进行大量的代码重写.

另外,还有Doctrine 2.我将使用1.2.4.再一次,我知道Doctrine 2真的很棒,但是我是否会坚持使用1.2.4?它似乎做了我们需要的一切.

感谢您的任何见解.

doctrine symfony1 symfony-1.4 symfony doctrine-orm

27
推荐指数
3
解决办法
7318
查看次数

无法使用Doctrine和Symfony2在MySQL中创建表

我正在使用MySql使用Symfony2和Doctrine ORM.

创建实体后,我无法创建表.它引发了一个例外.

anu@anu-bridge:/var/www/Symfony$ php app/console doctrine:schema:update --force --dump-sql

[Doctrine\DBAL\Schema\SchemaException]
The table with name 'product' already exists.  

doctrine:schema:update [--complete] [--dump-sql] [--force] [--em[="..."]]
Run Code Online (Sandbox Code Playgroud)

我试图放弃它,但仍然会抛出错误.

anu@anu-bridge:/var/www/Symfony$ php app/console doctrine:schema:drop --force
Dropping database schema...

[Doctrine\DBAL\Schema\SchemaException]         
The table with name 'product' already exists.  

doctrine:schema:drop [--dump-sql] [--force] [--full-database] [--em[="..."]]

[Doctrine\DBAL\Schema\SchemaException]         
The table with name 'product' already exists.
Run Code Online (Sandbox Code Playgroud)

数据库中没有表.清除了symfony和doctrine的所有缓存,但仍然是错误抛出.

Symfony2版本是2.0.1.

mysql symfony doctrine-orm

27
推荐指数
2
解决办法
2万
查看次数

如何阻止Doctrine 2在Symfony 2中缓存结果?

我希望能够检索实体的现有版本,以便将其与最新版本进行比较.例如,编辑文件,我想知道自从进入数据库以来该值是否已更改.

    $entityManager = $this->get('doctrine')->getEntityManager();
    $postManager = $this->get('synth_knowledge_share.manager');

    $repository = $entityManager->getRepository('KnowledgeShareBundle:Post');
    $post = $repository->findOneById(1); 

    var_dump($post->getTitle()); // This would output "My Title"
    $post->setTitle("Unpersisted new title");

    $existingPost = $repository->findOneById(1); // Retrieve the old entity

    var_dump($existingPost->getTitle()); // This would output "Unpersisted new title" instead of the expected "My Title"
Run Code Online (Sandbox Code Playgroud)

有谁知道如何绕过这个缓存?

php caching symfony doctrine-orm

27
推荐指数
2
解决办法
1万
查看次数

如何在Doctrine 2中设置日期?

我在教义实体中有一个名为"birthday"的字段.

我想创建一个使用doctrine添加到数据库的对象.

控制器内部:

$name = "John Alex";
$birthday = "11-11-90";
$student = new Student();
$student->setName($name);
$student->setBirthday(strtotime($birthday);
...
Run Code Online (Sandbox Code Playgroud)

但是当我试图坚持下去时,我得到了这个错误

Fatal error: Call to a member function format() on a non-object in /Library/WebServer/Documents/Symfony/vendor/doctrine-dbal/lib/Doctrine/DBAL/Types/DateType.php on line 44
Run Code Online (Sandbox Code Playgroud)

编辑:

我的实体:

/**
 * @var string $name
 *
 * @ORM\Column(name="name", type="string", length=255)
 */
private $name;

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

/**
 * Set birthday
 *
 * @param date $birthday
 */
public function setBirthday($birthday)
{
    $this->birthday = $birthday;
} …
Run Code Online (Sandbox Code Playgroud)

doctrine symfony doctrine-orm

27
推荐指数
2
解决办法
6万
查看次数

使用查询构建器删除Doctrine 2

我有两个实体关系OneToMany,ProjectServices.现在我想通过project_id删除所有服务.

第一次尝试:

$qb = $em->createQueryBuilder();
$qb->delete('Services','s');
$qb->andWhere($qb->expr()->eq('s.project_id', ':id'));
$qb->setParameter(':id',$project->getId());
Run Code Online (Sandbox Code Playgroud)

此尝试因异常而失败Entity Service does not have property project_id.这是真的,该属性不存在,它只在数据库表中作为外键.

第二次尝试:

$qb = $em->createQueryBuilder();
$qb->delete('Services','s')->innerJoin('s.project','p');
$qb->andWhere($qb->expr()->eq('p.id', ':id'));
$qb->setParameter(':id',$project->getId());
Run Code Online (Sandbox Code Playgroud)

这一个也是一个无效的DQL查询.

任何想法和例子都将受到欢迎.

php doctrine-orm

27
推荐指数
1
解决办法
6万
查看次数

FOS UserBundle无法登录

关于我的UserBundle,我又回到了另一个问题:通过Symfony2安装和配置FOS捆绑包时,一切都很完美,它甚至让我创建了2个正确插入我的数据库的用户.

但是,每次我尝试登录这些帐户时,都会出现以下错误

Warning: Erroneous data format for unserializing 'VillaPrivee\UserBundle\Entity\User' in /Users/Vianney/Projets/VillaPrivee/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php line 869
Run Code Online (Sandbox Code Playgroud)

这就是869行所指的:

/**
     * Creates a new instance of the mapped class, without invoking the constructor.
     *
     * @return object
     */
    public function newInstance()
    {
        if ($this->_prototype === null) {
            $this->_prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($this->name), $this->name));
        }

        return clone $this->_prototype;
    }
Run Code Online (Sandbox Code Playgroud)

这是我的用户实体:

namespace VillaPrivee\UserBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * …
Run Code Online (Sandbox Code Playgroud)

symfony doctrine-orm fosuserbundle

27
推荐指数
2
解决办法
1万
查看次数