我正在尝试访问从MySQL返回的数组元素
function CategorySlug(){
$category_slug = $this->f3->get('PARAMS.category_slug');
$categories = new Categories($this->db);
$cat = $categories->getBySlug($category_slug);
$this->f3->set('category',$cat);
$this->f3->set('view','mainCategory.html');
echo \Template::instance()->render('index.html');
}
public function getBySlug($category_slug)
{
$this->load(array('category_slug=?', $category_slug));
return $this->query;
}
当我尝试访问数组的元素时{{ @category[category_title] }}
或{{ @category.category_title }}
我有一个空页。
我做了一个{{var_dump(@category)}}只是为了看看我是否有来自DB的任何回复,我得到以下内容:
array(1) {
[0]=>
object(Categories)#15 (11) {
["db":protected]=>
object(DB\SQL)#9 (8) {
["uuid":protected]=>
string(12) "onvh9nsstjk8"
["pdo":protected]=>
object(PDO)#10 (0) {
}
["dsn":protected]=>
string(54) "mysql:host=localhost;dbname=dev.sp19789"
["engine":protected]=>
string(5) "mysql"
["dbname":protected]=>
string(26) "dev.sp19789"
["trans":protected]=>
bool(false)
["rows":protected]=>
int(1)
["log":protected]=>
string(381) " (0.6ms) SELECT `category_id`,`category_title`,`category_nav_title`,`category_slug`,`parent_category_id` FROM `categories` WHERE category_slug='Products'
"
}
["engine":protected]=>
string(5) "mysql"
["source":protected]=>
string(10) "categories"
["table":protected]=>
string(12) "`categories`"
["_id":protected]=>
NULL
["fields":protected]=>
array(14) {
["category_id"]=>
array(9) {
["type"]=>
string(16) "int(10) unsigned"
["pdo_type"]=>
int(1)
["default"]=>
NULL
["nullable"]=>
bool(false)
["pkey"]=>
bool(true)
["value"]=>
int(1)
["initial"]=>
int(1)
["changed"]=>
bool(false)
["previous"]=>
int(1)
}
["category_title"]=>
array(8) {
["type"]=>
string(12) "varchar(255)"
["pdo_type"]=>
int(2)
["default"]=>
NULL
["nullable"]=>
bool(false)
["pkey"]=>
bool(false)
["value"]=>
string(15) "Products"
["initial"]=>
string(15) "Products"
["changed"]=>
bool(false)
}
["category_nav_title"]=>
array(8) {
["type"]=>
string(12) "varchar(255)"
["pdo_type"]=>
int(2)
["default"]=>
NULL
["nullable"]=>
bool(false)
["pkey"]=>
bool(false)
["value"]=>
string(0) ""
["initial"]=>
string(0) ""
["changed"]=>
bool(false)
}
["category_slug"]=>
array(8) {
["type"]=>
string(12) "varchar(100)"
["pdo_type"]=>
int(2)
["default"]=>
NULL
["nullable"]=>
bool(false)
["pkey"]=>
bool(false)
["value"]=>
string(14) "products"
["initial"]=>
string(14) "products"
["changed"]=>
bool(false)
}
["parent_category_id"]=>
array(9) {
["type"]=>
string(16) "int(10) unsigned"
["pdo_type"]=>
int(1)
["default"]=>
NULL
["nullable"]=>
bool(false)
["pkey"]=>
bool(true)
["value"]=>
int(1)
["initial"]=>
int(1)
["changed"]=>
bool(false)
["previous"]=>
int(1)
}
}
["adhoc":protected]=>
array(0) {
}
["props":protected]=>
array(0) {
}
["query":protected]=>
array(1) {
[0]=>
object(Categories)#12 (11) {
["db":protected]=>
object(DB\SQL)#9 (8) {
["uuid":protected]=>
string(12) "onvh9nsstjk8"
["pdo":protected]=>
object(PDO)#10 (0) {
}
["dsn":protected]=>
string(54) "mysql:host=localhost;dbname=dev.sp19789"
["engine":protected]=>
string(5) "mysql"
["dbname":protected]=>
string(26) "dev.sp19789"
["trans":protected]=>
bool(false)
["rows":protected]=>
int(1)
["log":protected]=>
string(381) " (0.6ms) SELECT `category_id`,`category_title`,`category_nav_title`,`category_slug`,`parent_category_id` FROM `categories` WHERE category_slug='Products'
"
}
["engine":protected]=>
string(5) "mysql"
["source":protected]=>
string(10) "categories"
["table":protected]=>
string(12) "`categories`"
["_id":protected]=>
NULL
["fields":protected]=>
array(14) {
["category_id"]=>
array(9) {
["type"]=>
string(16) "int(10) unsigned"
["pdo_type"]=>
int(1)
["default"]=>
NULL
["nullable"]=>
bool(false)
["pkey"]=>
bool(true)
["value"]=>
int(1)
["initial"]=>
int(1)
["changed"]=>
bool(false)
["previous"]=>
int(1)
}
["category_title"]=>
array(8) {
["type"]=>
string(12) "varchar(255)"
["pdo_type"]=>
int(2)
["default"]=>
NULL
["nullable"]=>
bool(false)
["pkey"]=>
bool(false)
["value"]=>
string(15) "Products"
["initial"]=>
string(15) "Products"
["changed"]=>
bool(false)
}
["category_nav_title"]=>
array(8) {
["type"]=>
string(12) "varchar(255)"
["pdo_type"]=>
int(2)
["default"]=>
NULL
["nullable"]=>
bool(false)
["pkey"]=>
bool(false)
["value"]=>
string(0) ""
["initial"]=>
string(0) ""
["changed"]=>
bool(false)
}
["category_slug"]=>
array(8) {
["type"]=>
string(12) "varchar(100)"
["pdo_type"]=>
int(2)
["default"]=>
NULL
["nullable"]=>
bool(false)
["pkey"]=>
bool(false)
["value"]=>
string(14) "products"
["initial"]=>
string(14) "products"
["changed"]=>
bool(false)
}
["parent_category_id"]=>
array(9) {
["type"]=>
string(16) "int(10) unsigned"
["pdo_type"]=>
int(1)
["default"]=>
NULL
["nullable"]=>
bool(false)
["pkey"]=>
bool(true)
["value"]=>
int(1)
["initial"]=>
int(1)
["changed"]=>
bool(false)
["previous"]=>
int(1)
}
}
["adhoc":protected]=>
array(0) {
}
["props":protected]=>
array(0) {
}
["query":protected]=>
array(0) {
}
["ptr":protected]=>
int(0)
["trigger":protected]=>
array(0) {
}
}
}
["ptr":protected]=>
int(0)
["trigger":protected]=>
array(0) {
}
}
}
在我看来,当只有一个元素时,数组中有2个元素是相同的。
ATM,我只能显示类别标题,如{{ @category[0][category_title] }}
任何简化它的建议都会有所帮助
答案 0 :(得分:1)
方法getBySlug()
应该返回$this
,而不是其他:
public function getBySlug($category_slug)
{
$this->load(array('category_slug=?', $category_slug));
return $this;
}
答案 1 :(得分:1)
要返回普通对象,您应该执行强制转换
public function getBySlug($category_slug)
{
$this->load(array('category_slug=?', $category_slug));
return $this->cast();
}
答案 2 :(得分:0)
看来查询返回了两个结果。如果是这种情况,那么{{ @category[0][category_title] }}
确实是正确的解决方案。您可能还希望对数据库查询使用限制。
我确保使用Model与数据库交互,并将0索引结果返回给控制器。
答案 3 :(得分:0)
当您从数据库中获取行时,您将获得带有键的数组作为从db返回的行的数量,这些值是来自数据库的数据..因此,如果您选择仅获取一行,则数组将包含一个键(0)和类别值,因此数据将通过
获取@category[0][category_title]
只需在var中处理它:
$category = @category[0]