出于学习目的,我正在oop php中针对属性/广告开发cms。我有三个与数据透视表连接的表。
photos (name, extension, created_at, updated_at),
property_photo (property_id, photo_id),
properties (title, description, type_of_property, use_of_the_property, quadrature, location...)
当我尝试通过添加另一张照片来更新属性时,在我的数据透视表中,photo_id从photos表中获取了正确的id,但是我的property_id始终为0。现在我知道lastInsertId函数仅对INSERT无效,而对UPDATE不起作用不知道如何以不同的方式获取property_id。任何帮助表示赞赏。这是我在模型中的功能:
AdModel:
public function update_ad($data, $id)
{
$this->db->query('UPDATE properties SET title=:title, description=:description, type_of_property=:type_of_property, use_of_the_property=:use_of_the_property, quadrature=:quadrature, location=:location, price=:price, sales_clerk_info=:sales_clerk_info, booked=:booked, type_of_market=:type_of_market, type_of_payment=:type_of_payment WHERE id=:id');
$this->db->bind(':title', $data['title']);
$this->db->bind(':description', $data['description']);
$this->db->bind(':type_of_property', $data['type_of_property']);
$this->db->bind(':use_of_the_property', $data['use_of_the_property']);
$this->db->bind(':quadrature', $data['quadrature']);
$this->db->bind(':location', $data['location']);
$this->db->bind(':price', $data['price']);
$this->db->bind(':sales_clerk_info', $data['sales_clerk_info']);
$this->db->bind(':booked', $data['booked']);
$this->db->bind(':type_of_market', $data['type_of_market']);
$this->db->bind(':type_of_payment', $data['type_of_payment']);
$this->db->bind(':id', $id);
$this->db->execute();
$property_last_id = $this->db->lastId();
$dataimagecount = count($data['image']);
for ($i=0; $i < $dataimagecount ; $i++) {
$extension[$i] = explode('.',$data['image'][$i]);
$this->db->query('INSERT INTO photos (name, extension) VALUES (:name, :extension)');
$this->db->bind(':name', $extension[$i]['0']);
$this->db->bind(':extension', $extension[$i]['1']);
$this->db->execute();
$photo_last_id = $this->db->lastId();
$this->db->query('INSERT INTO property_photo (property_id, photo_id) VALUES (:property_id, :photo_id)');
$this->db->bind(':property_id', $property_last_id);
$this->db->bind(':photo_id', $photo_last_id);
$this->db->execute();
}
return true;
}
答案 0 :(得分:2)
您已经有properties
ID,您可以在UPDATE命令中使用它,并将其作为参数传递给此方法!!!
public function update_ad($data, $id)
{
$this->db->query('UPDATE properties
SET title=:title, description=:description,
type_of_property=:type_of_property,
use_of_the_property=:use_of_the_property,
quadrature=:quadrature,
location=:location,
price=:price,
sales_clerk_info=:sales_clerk_info,
booked=:booked,
type_of_market=:type_of_market,
type_of_payment=:type_of_payment
WHERE id=:id');
$this->db->bind(':title', $data['title']);
$this->db->bind(':description', $data['description']);
$this->db->bind(':type_of_property', $data['type_of_property']);
$this->db->bind(':use_of_the_property', $data['use_of_the_property']);
$this->db->bind(':quadrature', $data['quadrature']);
$this->db->bind(':location', $data['location']);
$this->db->bind(':price', $data['price']);
$this->db->bind(':sales_clerk_info', $data['sales_clerk_info']);
$this->db->bind(':booked', $data['booked']);
$this->db->bind(':type_of_market', $data['type_of_market']);
$this->db->bind(':type_of_payment', $data['type_of_payment']);
$this->db->bind(':id', $id);
$this->db->execute();
// --!! Changed code !!--
//$property_last_id = $this->db->lastId();
$dataimagecount = count($data['image']);
for ($i=0; $i < $dataimagecount ; $i++) {
$extension[$i] = explode('.',$data['image'][$i]);
$this->db->query('INSERT INTO photos
(name, extension)
VALUES (:name, :extension)');
$this->db->bind(':name', $extension[$i]['0']);
$this->db->bind(':extension', $extension[$i]['1']);
$this->db->execute();
$photo_last_id = $this->db->lastId();
$this->db->query('INSERT INTO property_photo
(property_id, photo_id)
VALUES (:property_id, :photo_id)');
$this->db->bind(':property_id', $id);
// changed code ^^^
$this->db->bind(':photo_id', $photo_last_id);
$this->db->execute();
}
return true;
}