我有一张桌子:
*CREATE TABLE IF NOT EXISTS `blogs_settings` (
`blog_id` int(11) NOT NULL AUTO_INCREMENT,
`owner_id` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`meta_description` text NOT NULL,
`meta_keywords` text NOT NULL,
`theme` varchar(25) NOT NULL DEFAULT 'default',
`is_active` tinyint(1) NOT NULL DEFAULT '1',
`date_created` int(11) NOT NULL,
PRIMARY KEY (`blog_id`),
KEY `owner_id` (`owner_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;*
Run Code Online (Sandbox Code Playgroud)
第二个表:
*CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(128) …
Run Code Online (Sandbox Code Playgroud) 我想INNER JOIN
在Zend2中的两个表之间做一个简单的操作.
具体来说,我想在Zend2中这样做:
SELECT * FROM foo, bar WHERE foo.foreign_id = bar.id;
我有一个FooTable
:
class FooTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function get($id)
{
$rowset = $this->tableGateway->select(function (Select $select) {
$select->from('foo');
});
}
}
Run Code Online (Sandbox Code Playgroud)
将$select->from('foo');
返回一个错误:
==> 由于此对象是在构造函数中使用表和/或模式创建的,因此它是只读的.
所以,我不能调整我的FROM语句来匹配FooTable
和之间的简单内连接BarTable
.
Zend或者我说这整个框架概念对我来说都是新的.一些示例基于tablegateway格式,您可以在其中定义Module.php中与该控制器相关的表的名称.
/* 'MYMODULE\Model\CompanyTable' => function($sm) {
$tableGateway = $sm->get('CompanyTableGateway');
$table = new CompanyTable($tableGateway);
return $table;
},
'CompanyTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Company());
return new TableGateway('rs_company', $dbAdapter, null, $resultSetPrototype);
},*/
Run Code Online (Sandbox Code Playgroud)
在其他示例中,Module.php中只有3行代码告诉适配器,然后通过__constuct()进行初始化
'MYMODULE\Model\CompanyTable' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$table = new CompanyTable($dbAdapter);
return $table;
},
Run Code Online (Sandbox Code Playgroud)
在课堂上__construc()
public function __construct(Adapter $adapter) {
$this->adapter = $adapter;
$this->resultSetPrototype = new ResultSet();
$this->resultSetPrototype->setArrayObjectPrototype(new Company());
$this->initialize();
}
Run Code Online (Sandbox Code Playgroud)
我无法理解的是如何在两者之间做出选择.
我试图从mysql切换到Zend/TableGateway类.
我不知道是否有一种方法,类似于mysql_insert_id,得到最后插入行的自动增量ID.
谷歌搜索周围,我发现了一些答案,数据库适配器的指向lastInsertId(),但是,这种方法似乎ZF2不再可用.另外:insert方法的返回值返回一个布尔值,而不是ZF1中的最后一个id.
目前我正在使用一个丑陋的解决方法.请参阅下面的代码.
是否有更好/推荐的获取身份证的方式?
table_1 {
id: integer, primary key, autoincremented
content: text
}
table_2 {
table_1_id: integer
other_content: text
}
// using mysql
$sql = "INSERT INTO table_1 (content) VALUES ('some text')";
$result = mysql_query($sql);
// check omitted
$id = mysql_insert_id();
$sql = "INSERT INTO table_2 (table_1_id, other_content) VALUES ($id, 'other text')";
$result = mysql_query($sql);
// using Zend - this is the code, I am currently using
//*************************************************************
// get_last_insert_id emulation; works only if content is unique
private …
Run Code Online (Sandbox Code Playgroud) lastinsertid zend-db last-insert-id zend-framework2 tablegateway
我在理解如何使用下面的代码实现DbSelect paginator时遇到了一些麻烦(现在它正在使用Iterator适配器,它不适用于ResultSet).
据我所知,它并不像我希望的那样直截了当.DbSelect期待一个Zend\Db\Sql\Select
和一个适配器.适配器是一个非问题,可以使用以下方法检索:
$this->newsContents()->getAdapter()
Run Code Online (Sandbox Code Playgroud)
但我无法Select
从我的TableGateway中获取一个对象而不重复我的查询代码.有没有简单的方法来解决这个问题?
NewsController.php
<?php
namespace News\Controller;
use Zend\Paginator\Paginator;
class NewsController extends \Application\Controller\WebsiteController
{
protected $newsTable;
protected $newsContents;
protected function newsTable()
{
return $this->getServiceLocator()->get('News\Model\NewsTable');
}
protected function newsContents()
{
return $this->getServiceLocator()->get('News\Model\NewsContentsTable');
}
protected function articleId()
{
return (int) $this->params()->fromRoute('id');
}
public function articleAction()
{
$article = $this->newsTable()->getArticle($this->articleId());
$pages = $this->newsContents()->getPages($this->articleId());
$paginator = new Paginator(new \Zend\Paginator\Adapter\Iterator($pages));
$paginator->setCurrentPageNumber($this->params()->fromRoute('page'));
return array(
'css' => 'news.css',
'article' => $article,
'paginator' => $paginator,
);
}
}
Run Code Online (Sandbox Code Playgroud)
NewsContentsTable.php
<?php
namespace …
Run Code Online (Sandbox Code Playgroud) 嗨,我试图掌握Zend 2,我在表网关中的where子句有一些问题.
下面是我的表类:
//module\Detectos\src\Detectos\Model\OperatingSystemTable.php
namespace Detectos\Model;
use Zend\Db\TableGateway\TableGateway;
class OperatingSystemsTable
{
public function findOs($userAgent)
{
$resultSet = $this->tableGateway->select();
foreach ($resultSet as $osRow)
{
//check if the OS pattern of this row matches the user agent passed in
if (preg_match('/' . $osRow->getOperatingSystemPattern() . '/i', $userAgent)) {
return $osRow; // Operating system was matched so return $oses key
}
}
return 'Unknown'; // Cannot find operating system so return Unknown
}
}
Run Code Online (Sandbox Code Playgroud)
模型是这样的:
class Detectos
{
public $id;
public $operating_system;
public $operating_system_pattern; …
Run Code Online (Sandbox Code Playgroud) 我想在我的验证课中获得ServiceLocator.我尝试从Controller实例获取它,但它返回null.
MyValidation.php
namespace Register\Validator;
use Zend\Validator\AbstractValidator;
use Register\Controller\RegisterController;
class MyValidation extends AbstractValidator {
/*
code...
*/
function isValid($value)
{
$controller = new RegisterController();
$sm = $controller->getServiceLocator();
$tableGateway = $sm->get('Register\Model\RegisterTable');
$tableGateway->myValidationMethod($value);
}
Run Code Online (Sandbox Code Playgroud)
}
Module.php
public function getServiceConfig()
{
return array(
'factories' => array(
'Register\Model\RegisterTable' => function($sm) {
$tableGateway = $sm->get('RegisterTableGateway');
$table = new RegisterTable($tableGateway);
return $table;
},
'RegisterTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new RegisterUser());
return new TableGateway('table-name', $dbAdapter, null, $resultSetPrototype);
},
),
);
} …
Run Code Online (Sandbox Code Playgroud) 我从ZendSkeletonApplication开始,添加了一个扩展Zend\Db\TableGateway\TableGateway的模型.我有以下方法:
public function findByType($type) {
$rowset = $this->select('type' => $type);
return $rowset;
}
Run Code Online (Sandbox Code Playgroud)
这有效,但现在如果我这样做:
$foo = $table->findBytype('foo');
$bar = $table->findBytype('bar');
Run Code Online (Sandbox Code Playgroud)
第一个工作,它执行的查询是:
SELECT * FROM table WHERE 'type' = 'foo'
Run Code Online (Sandbox Code Playgroud)
然而,第二个执行以下查询:
SELECT * FROM table WHERE 'type' = 'foo' AND 'type' = 'bar'
Run Code Online (Sandbox Code Playgroud)
这是预期的行为吗?如果是这样,我怎么能第二次调用该方法执行以下查询:
SELECT * FROM table WHERE 'type' = 'bar'
Run Code Online (Sandbox Code Playgroud)
提前致谢!