字段'cats'是Angular应用程序中的一个选择框,包含表cats中的数据,带有id、name和module_id。
Slim框架上用于添加项目的函数。。
function addItem($section) {
$request = 'Slim'Slim::getInstance()->request();
$item = json_decode($request->getBody());
$sql = "INSERT INTO " .$section. " (title, subtitle, slug, excerpt, originalDate, content, cats, published) VALUES (:title, :subtitle, :slug, :excerpt, :originalDate, :content, :cats, :published)";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("title", $item->title);
$stmt->bindParam("subtitle", $item->subtitle);
$stmt->bindParam("slug", $item->slug);
$stmt->bindParam("excerpt", $item->excerpt);
$stmt->bindParam("originalDate", $item->originalDate);
$stmt->bindParam("content", $item->content);
$stmt->bindParam("cats", $item->cats);
$stmt->bindParam("published", $item->published);
$stmt->execute();
$item->id = $db->lastInsertId();
$db = null;
print_r(json_encode($item));
}
catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
角度代码,用于选择框
<div class="col-sm-12 m-b-30">
<p class="f-500 c-black m-b-15">Categorias</p>
<div class="btn-group bootstrap-select show-tick">
<button type="button" class="btn selectpicker btn-default" ng-model="item.cats" data-toggle="dropdown" title="Selecione a Categoria" aria-expanded="false" data-html="1" data-multiple="1" bs-options="cats.id as cats.name for cats in categorias" bs-select>
Selecione a categoria <span class="caret"></span>
</button>
</div>
</div>
cats,返回mysql列上的Array
有什么想法吗?!
在线示例:http://45.55.73.98/angular-admin/api/v1/s/posts
已更新
$cats = null;
foreach ($item->cats as $cat) {
$cats .= $cat . ",";
}
和
$stmt->bindParam("cats", $cats);
正如评论中所述,问题的编辑中提供了解决方案,我只是把答案放在这里(根据评论中的建议进行了修改)。
this->cats
是一个数组-首先用将其转换为字符串
$cats=implode(',',$item->cats);
然后把他绑起来:
$stmt->bindParam("cats", $cats);