1. Trang chủ
  2. » Công Nghệ Thông Tin

Thiết kế mạng xã hội với PHP - 34

10 14 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 5,25 MB

Nội dung

Tham khảo tài liệu ''thiết kế mạng xã hội với php - 34'', công nghệ thông tin, đồ họa - thiết kế - flash phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả

Groups If we are setting the first post, our controllers can access this post object by calling the getFirstPost method, and then calling the appropriate public methods on the post object /** * Return the object for the first post, for setting fields * @return Object */ public function getFirstPost() { return $this->post; } We have a number of setter methods, as standard /** * Set the group this topic should be part of * @param int $group * @return void */ public function setGroup( $group ) { $this->group = $group; } /** * Set the creator of the topic * @param int $creator * @return void */ public function setCreator( $creator ) { $this->creator = $creator; } /** * Set the name of the topic * @param String $name * @return void */ public function setName( $name ) { $this->name = $name; } [ 312 ] Download from Wow! eBook Chapter 10 We have our save method, which if appropriate, also saves the post once the topic has been created /** * Save the topic into the database * @return void */ public function save() { if( $this->id > ) { $update = array(); $update['creator'] = $this->creator; $update['name'] = $this->name; $update['group'] = $this->group; $this->registry->getObject('db')->updateRecords( 'topics', $update, 'ID=' $this->id ); } else { $insert = array(); $insert['creator'] = $this->creator; $insert['name'] = $this->name; $insert['group'] = $this->group; $this->registry->getObject('db')->insertRecords( 'topics', $insert ); $this->id = $this->registry->getObject('db')->lastInsertID(); if( $this->includeFirstPost == true ) { $this->post->setTopic( $this->id ); $this->post->save(); } } } Next, we have a getter for the name property, and also a toTags method, which is now almost a standard for most of our models /** * Get the name of the topic */ public function getName() { return $this->name; } [ 313 ] Download from Wow! eBook Groups /** * Convert the topic data to template tags * @param String $prefix prefix for the template tags * @return void */ public function toTags( $prefix='' ) { foreach( $this as $field => $data ) { if( ! is_object( $data ) && ! is_array( $data ) ) { $this->registry->getObject('template')->getPage()->addTag( $prefix.$field, $data ); } } } /** * Get the group this topic was posted within * @return int */ public function getGroup() { return $this->group; } Finally, we have a delete method, which in addition to deleting the current topic from the database, also removes any posts related to it in the posts table /** * Delete the current topic * @return boolean */ public function delete() { $sql = "DELETE FROM topics WHERE ID=" $this->id; $this->registry->getObject('db')->executeQuery( $sql ); if( $this->registry->getObject('db')->affectedRows() > ) { $sql = "DELETE FROM posts WHERE topic=" $this->id; $this->registry->getObject('db')->executeQuery( $sql ); $this->id =0; return true; } [ 314 ] Download from Wow! eBook Chapter 10 else { return false; } } } ?> The group itself With the models for topics and posts (which we will be using shortly) in place, we can now focus our attention on the group itself, as the group will need to make use of these models so that users of the groups can communicate and collaborate with one another Group table The first stage, as with the other aspects of our social network, is the database table We've already discussed what information the group needs to store; the following database structure simply formalizes that: Field Type Description ID Integer, Auto-increment, Primary Key Internal ID / reference for the group Name Varchar Name of the group Description Longtext Detailed description of the group Type ENUM The type of the group Creator Integer The user who created the group Created Timestamp The time the group was created Active Boolean If the group is active, gives us the ability to de-activate groups later without deleting them Model The model required for groups (models/group.php) is fairly standard with a few minor additions We have some validation on the type of group, and we also have a method to cache a query of topics posted in the group

Ngày đăng: 08/05/2021, 17:31