我有一张桌子:
*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 ;*
第二张表:
*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) NOT NULL,
`sex` tinyint(1) NOT NULL,
`birthday` date NOT NULL,
`avatar_id` int(11) DEFAULT NULL,
`user_level` tinyint(1) NOT NULL DEFAULT '1',
`date_registered` int(11) NOT NULL,
`is_active` tinyint(1) NOT NULL DEFAULT '0',
`is_banned` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`user_id`),
KEY `is_active` (`is_active`),
KEY `user_level` (`user_level`),
KEY `is_banned` (`is_banned`),
KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;*
我如何从blogs_settings表中选择所有字段,并在blogs_settings.owner_id = users.user_id
上使用ZF2中的TableGateway仅加入users表中的'username'字段。提前致谢。非常感谢您的帮助。
namespace Object\Model;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
class BlogsSettingsTable {
protected $tableGateway;
protected $select;
public function __construct(TableGateway $tableGateway) {
$this->tableGateway = $tableGateway;
$this->select = new Select();
}
public function getBlogs($field = '', $value = '') {
$resultSet = $this->tableGateway->select(function(Select $select) {
$select->join('users', 'blogs_settings.owner_id = users.user_id', array('username'));
});
return $resultSet;
}
public function getBlog($blogID) {
$id = (int) $blogID;
$rowset = $this->tableGateway->select(array('blog_id' => $id));
$row = $rowset->current();
if (!$row) {
throw new Exception('Could not find row with ID = ' . $id);
}
return $row;
}
public function addBlog(BlogsSettings $blog) {
$data = array(
'owner_id' => $blog->owner_id,
'title' => $blog->title,
'meta_description' => $blog->meta_description,
'meta_keywords' => $blog->meta_keywords,
'theme' => $blog->theme,
'is_active' => $blog->is_active,
'date_created' => $blog->date_created,
);
$this->tableGateway->insert($data);
}
public function deleteBlog($blogID) {
return $this->tableGateway->delete(array('blog_id' => $blogID));
}
}
这样,它执行以下查询:
选择blogs_settings
。*,users
。username
AS username
来自blogs_settings
内部加入users
ON blogs_settings
。{ {1}} = owner_id
。users
但是resultSet不包含已加入的“users”表中的用户名字段。但是,当我在phpmyadmin中运行查询时,一切正常,我加入了'users'表中的'username'字段。有什么问题?
编辑2 好的,我现在尝试了以下内容:
user_id
执行的查询是:
public function getBlogs() {
$select = $this->tableGateway->getSql()->select();
$select->columns(array('blog_id', 'interest_id', 'owner_id', 'title', 'date_created'));
$select->join('users', 'users.user_id = blogs_settings.owner_id', array('username'), 'left');
$resultSet = $this->tableGateway->selectWith($select);
return $resultSet;
}
当我将它运行到phpmyadmin时,它会加入users表中的用户名字段。在zf2中时,它不会。
这是整个对象的转储:
SELECT `blogs_settings`.`blog_id` AS `blog_id`, `blogs_settings`.`interest_id` AS `interest_id`, `blogs_settings`.`owner_id` AS `owner_id`, `blogs_settings`.`title` AS `title`, `blogs_settings`.`date_created` AS `date_created`, `users`.`username` AS `username` FROM `blogs_settings` LEFT JOIN `users` ON `users`.`user_id` = `blogs_settings`.`owner_id`
向上......任何想法?
答案 0 :(得分:13)
如果你正在使用TableGateway,你可以像这样选择连接
$sqlSelect = $this->tableGateway->getSql()->select();
$sqlSelect->columns(array('column_name'));
$sqlSelect->join('othertable', 'othertable.id = yourtable.id', array(), 'left');
$resultSet = $this->tableGateway->selectWith($sqlSelect);
return $resultSet;
答案 1 :(得分:13)
添加@samsonasik的答案并在评论中解决问题。您无法从该语句返回的内容中获取连接值。该语句返回不具有连接行的模型对象。您需要将它作为SQL执行,并将其作为原始SQL进行准备,并将每个结果行作为数组而不是对象返回:
$sqlSelect = $this->tableGateway->getSql()->select();
$sqlSelect->columns(array('column_name_yourtable'));
$sqlSelect->join('othertable', 'othertable.id = yourtable.id', array('column_name_othertable'), 'left');
$statement = $this->tableGateway->getSql()->prepareStatementForSqlObject($sqlSelect);
$resultSet = $statement->execute();
return $resultSet;
//then in your controller or view:
foreach($resultSet as $row){
print_r($row['column_name_yourtable']);
print_r($row['column_name_othertable']);
}
答案 2 :(得分:2)
您必须在BlogsSetting Model中包含用户名字段,该字段用作BlogsSettingTable(TableGateway)中的模型
class BlogsSetting {
public $blog_id;
public $interest_id;
public $owner_id;
public $title;
public $meta_description;
public $meta_keywords;
public $theme;
public $is_active;
public $date_created;
public $username;
public function exchangeArray($data)
{
// Create exchangeArray
}
}
希望这有帮助
答案 3 :(得分:2)
加入和其中条款与 tableGateway 的确切需要。
public function getEmployeefunctionDetails($empFunctionId) {
$empFunctionId = ( int ) $empFunctionId;
//echo '<pre>'; print_r($this->tableGateway->getTable()); exit;
$where = new Where();
$where->equalTo('FUNCTION_ID', $empFunctionId);
$sqlSelect = $this->tableGateway->getSql()->select()->where($where);
$sqlSelect->columns(array('FUNCTION_ID'));
$sqlSelect->join('DEPARTMENTS', 'DEPARTMENTS.DEPARTMENT_ID = EMPLOYEE_FUNCTIONS.DEPARTMENT_ID', array('DEPARTMENT_ID','DEPARTMENT_NAME'), 'inner');
$sqlSelect->join('ROLES', 'ROLES.ROLE_ID = EMPLOYEE_FUNCTIONS.ROLE_ID', array('ROLE_ID','ROLE_NAME'), 'inner');
//echo $sqlSelect->getSqlString(); exit;
$resultSet = $this->tableGateway->selectWith($sqlSelect);
if (! $resultSet) {
throw new \Exception ( "Could not find row $empFunctionId" );
}
return $resultSet->toArray();
}
答案 4 :(得分:1)
在继承自AbstractTableGateway的类中,你可以像这样使用Select with Closure:
use Zend\Db\Sql\Select;
...
public function getAllBlockSettings()
{
$resultSet = $this->select(function(Select $select) {
$select->join('users', 'blogs_settings.owner_id = users.user_id', array('username'));
});
return $resultSet;
}
答案 5 :(得分:1)
试一试:
namespace Object\Model;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Sql\Select;
class BlogsSettingsTbl extends AbstractTableGateway {
public function __construct($adapter) {
$this->table = 'blogs_settings';
$this->adapter = $adapter;
$this->initialize();
}
public function fetchAll() {
$where = array(); // If have any criteria
$result = $this->select(function (Select $select) use ($where) {
$select->join('users', 'blogs_settings.owner_id = users.user_id', array('username'));
//echo $select->getSqlString(); // see the sql query
});
return $result;
}
}
在Module.php中添加'getServiceConfig()':
'Object\Model\BlogsSettingsTbl' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$table = new BlogsSettingsTbl($dbAdapter); // <-- also add this to 'USE' at top
return $table;
},
答案 6 :(得分:0)
$this->username= (!empty($data['username'])) ? $data['username'] : null;
您应该将以上行添加到exchangeArray()方法中。 希望它有所帮助