我实际上陷入了三层结构.我浏览了互联网,发现了两个术语"数据库抽象层"和"数据访问层".
两者有什么不同?
我正在使用提供ORM和DBAL的Doctrine.
他们之间有什么区别?
应该如何决定何时使用哪个?
使用示例文档,我能够获得这样的查询.
SELECT
f.foo,
b.bar
FROM Foo f
LEFT JOIN Bar b
WHERE
f.foo = 20
AND b.bar IN ?
Run Code Online (Sandbox Code Playgroud)
使用DBAL,此代码返回结果.
$result = $this->connection->executeQuery(
$SQL,
array($bar_array),
array(\Doctrine\DBAL\Connection::PARAM_INT_ARRAY)
)->fetchAll();
Run Code Online (Sandbox Code Playgroud)
我想搜索f.foo作为单个整数参数以及查找IN语句,但我还没有弄清楚如何使它运行,因为所有示例文档都将数组作为唯一参数.
如何使用Doctrine\DBAL将PHP的DateTime对象作为数据库字段的值传递?
$ DB是Doctrine\DBAL\Connection实例.
$DB->insert('table_name', [
'field' => new \DateTime(),
]);
// Catchable fatal error: Object of class DateTime could not be converted to string
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用,文档很少.
我确信您可以使用其他DBAL方法直接提供DateTime对象,是否可以使用insert()执行此操作?
我得到一个错误,如使用Symfony2的数据库操作.
SQLSTATE[HY000] [2002] Connection refused
Run Code Online (Sandbox Code Playgroud)
parameters.yml
parameters:
database_driver: pdo_mysql
database_host: 127.0.0.1
database_port: '8889'
database_name: symfony
database_user: root
database_password: root
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
locale: tr
secret: ef9b4381fe75208f060b7c786951242bebcfb3c2
database_path: /private/var/mysql/mysql.sock
Run Code Online (Sandbox Code Playgroud)
和控制台:
Kemal-Karakass-MacBook-Pro:~ kemalkarakas$ locate mysql.sock
/private/var/mysql/mysql.sock
Run Code Online (Sandbox Code Playgroud)
我该如何解决错误?
在我执行这样的查询时,在doctrine DBAL2中:
<?php
$connection = $this->getDatabaseConnection();
$sql = "SELECT page_url
FROM cms_user_page
WHERE site_id = :siteid
AND active = '1'
";
$stmt = $connection->prepare($sql);
$stmt->bindValue("siteid", $id);
$stmt->execute();
return $stmt->fetchAll();
?>
Run Code Online (Sandbox Code Playgroud)
我得到这样的结果:
Array
(
[0] => Array
(
[page_url] => index.php?action=login
)
[1] => Array
(
[page_url] => index.php?action=shoppingcart
)
[2] => Array
(
[page_url] => index.php?action=products
)
)
Run Code Online (Sandbox Code Playgroud)
Array
(
[0] => index.php?action=login
[1] => index.php?action=shoppingcart
[2] => index.php?action=products
)
Run Code Online (Sandbox Code Playgroud)
我在文档中找不到有关获取模式的任何信息.我可以做一个数组地图.但在我看来这是开销.
我正在使用doctrine DBAL,并且由于queryBuilder而导致SQL查询出现问题.
$builder = $this->getConnection()->getQueryBuilder();
$builder->select(['id','name','type'])
->from('table')
->where('id='.(int)$value)
->setMaxResults(1);
$builder->andWhere($builder->expr()->in('type', ['first','second']));
echo(builder->getSQL());
$data = $builder->execute()->fetchRow();
Run Code Online (Sandbox Code Playgroud)
并获得SQL
SELECT id, name, type FROM table WHERE (id=149) AND (type IN (first,second)) LIMIT 1
Run Code Online (Sandbox Code Playgroud)
这就是问题,我需要(输入IN(第一,第二))被编码为字符串(类型IN('first','second'))
如何以正确的方式使用查询构建器?
我正在使用Symfony2开发应用程序.Symfony2正在使用Doctrine 2进行DBAL和ORM.据我所知,Doctrine2没有支持BLOB数据类型.但是,我想通过自定义数据类型映射实现BLOB支持:
http://www.doctrine-project.org/docs/dbal/2.0/en/reference/types.html
但是我很难理解这部分应该去哪里.
<?php
Type::addType('money', 'My\Project\Types\MoneyType');
$conn->getDatabasePlatform()->registerDoctrineTypeMapping('MyMoney', 'money');
Run Code Online (Sandbox Code Playgroud)
有人在经历吗?
我需要BLOB类型的原因是我想从现有MySQL数据库导入映射.
在一些现有代码上,我有以下语句(经过相当长的查询构建练习):
return $statement->fetchAll(
DBAL\FetchMode::CUSTOM_OBJECT,
PublishedLead::class
);
Run Code Online (Sandbox Code Playgroud)
该作品(到目前为止),但我现在都可以看到fetchAll(),并FetchMode都因为DBAL 2.11弃用:
// ResultStatement::fetchAll()
/*
* @deprecated Use fetchAllNumeric(), fetchAllAssociative()
* or fetchFirstColumn() instead.
*/
Run Code Online (Sandbox Code Playgroud)
// FetchMode
/*
* @deprecated Use one of the fetch- or iterate-related
* methods on the Statement
*/
Run Code Online (Sandbox Code Playgroud)
为了保持我的代码尽可能向前兼容,如何编写它以将结果提取到自定义对象中?我是否必须根据结果编写自定义保湿逻辑,或者 DBAL 可以为我做这件事吗?
我还在研究用于测试我的symfony2控制器的PHP单元测试.我的测试类是WebTestCase的派生,测试正在进行GET或POST请求,检查一切是否正常.我想测试所有底层,但我不想用测试弄乱我的数据库.我不想使用模拟ups,而是使用内存中的SQLite数据库,我可以在其中设置测试场景以检查所有修改.我发现了很多关于如何使用doctrine 1.x做到这一点的提示,但它们不再起作用了.所以我想要这样的东西:
class BlahblahTest extends WebTestCase {
public function testXXXYYY() {
// 1. Setup a new database with SQLite:memory:
// 2. create the database and all tables according the entities in my project
$this->createTestScenario(); // 3.
$crawler = $this->client->request('GET', '/testpage'); // 4.
// 5. Lots of checks against the database and / or the $crawler data
}
}
Run Code Online (Sandbox Code Playgroud)
有机会获得这项工作吗?
在此先感谢Hennes