【发布时间】:2019-06-17 14:33:08
【问题描述】:
出于学习目的,我正在为 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 从照片表中获取正确的 id,但我的 property_id 始终为 0。现在我知道 lastInsertId 函数仅在 INSERT 上对 UPDATE 不起作用,但我不知道如何以不同的方式获取 property_id。任何帮助表示赞赏。这是我在模型中的功能:
广告模型:
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;
}
【问题讨论】: