Posted by: calsunshine | August 24, 2011

How to create a model and get data from MySQL to display in SE4

Let me cover how to create model, controller and view files to get data from a MySQL table.


First, go to phpmyadmin page and create a new table in the social engine database. You can save the following sql code as trip.sql and import it to your mysql database to auto create the table and test data. It will create a table named engine4_traveling_trips, which stores the trip information belongs to a user. You must change “engine4” to your database prefix. “traveling” is the module name I created last time. “trips” is the table name, also our model name. It also inserts 7 row of test data.

-- phpMyAdmin SQL Dump
-- version 3.2.4
-- Host: localhost
-- Generation Time: Aug 23, 2011 at 10:56 PM
-- Server version: 5.1.44
-- PHP Version: 5.3.1

-- Table structure for table `engine4_traveling_trips`

CREATE TABLE IF NOT EXISTS `engine4_traveling_trips` (
  `trip_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `trip_name` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `user_id` int(11) unsigned NOT NULL,
  `thumb_url` varchar(2083) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `creation_date` datetime NOT NULL,
  `modified_date` datetime NOT NULL,
  PRIMARY KEY (`trip_id`),
  KEY `user_id` (`user_id`)

-- Dumping data for table `engine4_traveling_trips`

INSERT INTO `engine4_traveling_trips` (`trip_id`, `trip_name`, `user_id`, `thumb_url`, `creation_date`, `modified_date`) VALUES
(1, '旧金山之旅', 1, 'http://localhost/socialengine416/public/traveling/trips/trips_San_Francisco_thumb-10000000001.jpg', '2011-08-18 00:00:00', '2011-08-18 00:00:00'),
(2, '伦敦七天游', 1, 'http://localhost/socialengine416/public/traveling/trips/trips_London_thumb_10000000002.jpg', '2011-08-18 14:09:46', '2011-08-18 14:09:51'),
(3, '浪漫之都巴黎10日蜜月游', 1, 'http://localhost/socialengine416/public/traveling/trips/trips_Paris_thumb_1000000003.jpg', '2011-08-18 00:00:00', '2011-08-18 00:00:00'),
(4, '哥斯达黎加探险之旅', 2, 'http://localhost/socialengine416/public/traveling/trips/trips_Costa_Rica_thumb_1000000004.jpg', '2011-08-18 00:00:00', '2011-08-18 00:00:00'),
(5, '洛杉矶夏日一周游', 2, 'http://localhost/socialengine416/public/traveling/trips/trips_Los_Angeles_thumb_1000000005.jpg', '2011-08-18 16:40:36', '2011-08-18 16:40:39'),
(6, '香港国庆七天畅游', 3, 'http://localhost/socialengine416/public/traveling/trips/trips_Hong_Kong_thumb_1000000006.jpg', '2011-08-18 16:42:21', '2011-08-18 16:42:21');

Now we have the table, let’s create a model to represent the table and the row class. If you have followed my custom module tutorial last time, you will find there is a /Module/Traveling/Model/ folder and a DbTable subfolder. Create “trip.php” under “Model” folder and create “trips.php” under DbTable subfolder.

Code for “trip.php”:

    class Traveling_Model_Trip extends Core_Model_Item_Abstract
        protected $_owner_type = 'user';

The name of the class is in the format of “Modulename_Model_Tablename”. I don’t think the $_owner_type line is needed but just in case. This class represents each row of the trip table, so its name is singular.

Code for “trips.php”:

class Traveling_Model_DbTable_Trips extends Engine_Db_Table
        protected $_rowClass = 'Traveling_Model_Trip';

This class represents the whole trips table, so it is plural.


Now open the tripController.php created under “/Module/Traveling/controllers/”. Add a new action called viewallAction(), which is to view all the trips belong to a user.

viewallAction() code:


class Traveling_TripController extends Core_Controller_Action_Standard
    public function indexAction()
         // some code here...

    // view all trips of a user
    public function viewallAction()
        // get the table
        $table = Engine_Api::_()->getDbtable('trips', 'traveling');
        // filtering, get only the current user's trips
        $select = $table->select()
                        ->where("user_id = $id", 1)
        // get the data
        $result = $table->fetchAll($select);
        $this->view->trips = $result;

To get the table, use “Engine_Api::_()->getDbtable(‘tablename’, ‘modulename’)”. Then use select() method to define the select query like in SQL. where() defines the criteria and order() defines sorting. To execute the query use fetchAll(). These are all Zend Framework functions. Some documentation here:

In this way, the objects returned are a collection of the Traveling_Model_Trip object defined above in the trip.php model. Use $this->view->anyvariablename to pass the object to the view.


Now let’s display the results in the view. Create a subfolder “trip” for tripController under “/Traveling/views/scripts/”. Then create a view file viewall.tpl under the trip subfolder. The naming convention is actionname.tpl.

Code for viewall.tpl:

foreach($this->trips as $trip) { 
    echo "Trip id is: ".$trip->trip_id." and trip name is: ".$trip->trip_name.".";

Then go to url “http://localhost/sitename/index.php/traveling/trip/viewall/&#8221; or “http://localhost/sitename/traveling/trip/viewall/&#8221;. You should see the trip id and trip name being displayed. The naming convention of the url is “http://servername/sitename/modulename/controllername/actionname/&#8221;.

Posted by: calsunshine | August 21, 2011

Tutorial on creating a custom Module in SE4

Here is how to create a custom module in SE 4.1.6.

  1. In the admin page, go to Package Manager->Developer SDK->Create a Package
  2. Select Type Module and type in name and rest, I picked ‘traveling’.
  3. Download & save the module-traveling-1.0.0.tar when prompt.
  4. Go back to package manager and install the above .tar file.
  5. Now go to the “/socialengine/Application/Modules/Traveling” folder. You should see a bunch of files and folders generated.
  6. MVC are located under “Model”, “views” and “controllers” folder, respectively.
  7. If not exists, create an “IndexController.php” file under the controllers folder. Then paste in the following code:

Code for IndexController.php:

class Traveling_IndexController extends Core_Controller_Action_Standard
     public function indexAction()
 $this->view->msg = 'Hello World';

Explanation: the controller class name has to be Modulename_ControllernameController. In the Index controller, I created an index action, which is the default action. Then I created a variable named “msg” and passed it to the view object so it display it.

8.  Then create a view for the index action. Create an ‘index’ folder under “/socialengine/Application/Modules/Traveling/views/scripts/index”. Then create an index.tpl file in this folder.

9. Code for index.tpl:

<?php echo $this->msg; ?>

Basically  it is just output the msg variable I passed to the view from the indexAction in the controller.

10. Then go to the url: “http://localhost/socialengine416/index.php/traveling/index&#8221; and you should see ‘Hello World’. The format of the url to invoke the action is “http://sitename/index.php/modulename/controllername/actionname&#8221;. Since index is the default action, it can be omitted. Otherwise, the full url should be “http://localhost/socialengine416/index.php/traveling/index/index&#8221;.

You’ll notice there is no Model involved yet. I’ll cover that in the next post, i.e., how to create Model and get data from MySql and interact with it.

Posted by: calsunshine | August 10, 2011

Hello World in SE4

Tutorial: How to create a Hello World page in Social Engine. Tested in SE 4.1.6.

  • Go to Admin Panel->Package Manager->Developer SDK->Create a Package.
  • Choose Type ‘Widget’ and fill out the rest of the form. Note that the name can only be Lowercase, alphanumeric, and hyphens (“-“).
  • Then it will save a .tar file to your disk. For example, a “widget-travel-4.0.0.tar” file. travel is my widget name.
  • Go back to Package Manager->Install New Packages and browse the .tar file generated.
  • Follow the instructions to finish the install.
  • By default, an empty widgets will install 4 files to the SE folders. In my case, these 4 files and 1 folder are installed:
  1. \SocialEngine415\application\packages\widget-travel-4.0.0.json
  2. \SocialEngine415\application\widgets\travel\ folder.
  3. \SocialEngine415\application\widgets\travel\Controller.php
  4. \SocialEngine415\application\widgets\travel\index.tpl
  5. \SocialEngine415\application\widgets\travel\manifest.php

If you cannot generate or install the .tar file due to some permission problem. Download the “widget-travel-4.0.0.tar” I uploaded to the Dropbox share folder. Then unzip the .tar and place the respective files into their folders.

  • After installation of your widget, go to Admin Panel->Layout Editor. I created a new page called Hotels.
  • Then browse to the Available Blocks section and under Widgets you will see your widget there. In my case, there is a Travel widget.
  • I can then drag the Travel widget onto the Hotels page I created. Save the page.
  • Now let’s modify the view. Open \SocialEngine415\application\widgets\travel\index.php. Replace the content with the following:

    echo “<H1><b>Hello World!</b></H1>”;

Posted by: cadrianchan | August 5, 2011

Framework updates

Found an online free version of the RailsSpace book you mentioned: – i put rails 2.3 here coz it seems not all frameworks support rails 3 yet.
Also, I’ve been looking at the rails for zombies tutorials as well 🙂

Here’s a list of social networking frameworks that I’ve found:

btw, have you heard of this?
Also, there’re other templates from Joomla and Drupal as I mentioned (of coz we still gotta learn a real programming language for our real site).

I read a very insightful industry report from BCG (Boston Consulting Group) regarding China Travel Market. It gave me a fundamental understanding of the current situation and future prospect of China’s travel industry, as well as the online travel planning sector. You can download it:

Chinese Version:

English Version:

I have copied some key points here:

  1. 2013年,中国有望赶超日本,成为全球第二大旅游市场。
  2. 中国游客与西方游客的差别相当显著。例如,中国的旅游需求主要来自于非常渴望见识新世界的年轻人,而西方的旅游需求主要来自于年 长者,他们往往有更多的时间和金钱进行旅行。大部分中国游客不到40岁。
  3. 中国游客规划行程所花的时间更少。
  4. 旅游规划 —— 由于大多数中国游客 的休闲游经验较少,因此他们在旅游网站上寻求建议、 搜集信息并在线预订。超过一半的受访者把网上信息列为他们最信赖的旅行计划来源,而只有三分之一相信口碑。这可能是因为许多人不认识那些曾游历过他们想去 之地的游客。对于出国旅游,人们更愿意依赖旅行社, 不仅是为了方便(政府法规要求游客去某些目的地需要 取得签证,而且申请流程可能不确定且十分繁琐),同 时也是因为旅行社能够帮助弥合文化差异和克服语言障碍。
  5. 中国游客却将近一半旅游预算用于购物。
  6. 随着中国游客的经验越来越丰富,他们很可能会通 过网上搜索和预订网站自己安排交通和住宿,而不是依靠 旅行社,尤其是国内休闲游。但在中国,旅游业非常分散且不 具差异性,消费者一般不信任旅行社和广告。此外,对 于大多数中国消费者而言,能够提供建议的亲友不多。 因此,特别是经验丰富的中国游客会花大量时间依靠自 己来寻找并比较信息和价格。这类规划大部分是在网上完成,包括研究目的地、预订交通工具以及预订国内游 的酒店房间。下载和了解网上的复杂信息、比 较价格并确定哪些评论者更加可信可能需要花费几个小 时,甚至几天时间。一站式 的、用户友好的旅游网站市场日益成长,这些网站能够 为游客提供可信赖的信息和服务。

The above chart is a typical Chinese tourist planning for travel online. She spent days visiting a dozen websites to seal the deal (Just like how I planned my Sanya trip!!!).

Therefore, I think the website to be built must integrate at least the functionality of step 1, 2 and 3 in the above chart, in order to satisfy the growing needs for Chinese tourists, which I believe is also the model of NileGuide. So far, I have seen none such website in China yet. Hence, this is a once-in-a-blue-moon chance to gran before others come up with such a website!!!

Posted by: cadrianchan | July 30, 2011

The potential of big data

The potential of big data

I find the bottom part, statistics about personal location technology, particularly worth reading for LBS startups.

Posted by: cadrianchan | July 29, 2011

When engineering meets sports

Technology will one day be determining in-game strategy and making sideline calls themselves. During a presentation on the second day of the 2011 MIT Sloan Sports Analytics Conference, Kamil said he figures it’ll arrive within the next 20 years.

What if the technology is used in sports gambling / sports simulation? 

Posted by: calsunshine | July 29, 2011

Google’s Newest Hotel Finder;d=2011-08-04;n=1

I wonder if we can leverage this new service in China. At least I think these elements are interesting:

  1. Let users define the areas to search on the map.
  2. Show popular spots around.
  3. Add hotels interested to a short list.
  4. Hotel pictures well organized.
  5. Fast!!!

We should get this book! Now in pre-order:


Tested principles for transforming an idea into a fully operational company

Startup Weekend—the organization behind 54-hour events where developers, designers, marketers, and startup enthusiasts come together to share ideas, form teams, build products, and create startups—has spawned both a global initiative in entrepreneurship as well as numerous successful startups. Startup Weekend, the book, contains best practices, lessons learned, and empowering examples derived from the organization’s experiences for individuals and small organizations to follow as they launch businesses. Each of the key beliefs outlined has been tested by Startup Weekend and has yielded powerful results.

The principles described in each chapter will give any business idea a greater chance for success.

  • Chapter topics include trust and empowerment, flexible organizational structures, the power of experiential education, action-based networking, and much more
  • Describes consequences for startup development as entrepreneurs and founders begin doing much more, even faster
  • Profiles successful Startup Weekend companies, including two powerful examples: Memolane, an application that captures a user’s online life in one timeline making it easy for users to travel back in time and relive memories; and Foodspotting, a mobile and desktop app that allows users to find and share the foods they love

Apply these simple actionable principles to launch your own startup revolution.

Posted by: calsunshine | July 27, 2011


项目领域: 移动互联网|电子商务|云计算|游戏|其他
团队所在地: 如果团队成员在多个城市,请填写主要联系人所在地
1.1 描述你的商业创意,说明你的创意或产品能满足用户的何种核心需求?在没有你的产品时用户是如何解决该需求的?(最多150字)简要并准确地介绍你的产品,描述产品的市场机会与规模。描述你们正在解决的问题和你们自己的解决方法。

1.2 你的创意或者产品的规划是怎样?(最多300字)描述你会选择怎样的切入点,会有怎样的扩展方向,产品最后会是什么样子。

1.3 你产品的目标人群是什么,为什么选择这些人?打算采用什么推广方式获得早期用户?(最多300字)

1.4 描述用户使用产品的流程和体验?(最多300字)简要描述用户在你的产品上可以通过怎样的操作实现怎样的功能,得到怎样的体验。

1.5 打算用什么技术实现该创意,技术是否具备可扩展性?(最多150字)说明你打算采用的技术,选择的原因以及你的团队具备的能力。

1.6 现在还有哪些公司在做相似的产品?你怎么看待他们的产品?(最多300字)指出你的直接竞争者,并展示你是如何研究他们的产品。

1.7 你的产品或服务有何创新点?如何做到差异化?(最多150字)

1.8 你的产品将以何种方式盈利?(最多150字)例如:从广告,消费勘察或相关调查,佣金,电子商务,订购,软件销售,专利许可,虚拟商品等方面。

1.9 你的团队为这个创意已经奋斗多长时间?: 刚开始 1-3个月3-6个月

1.10 你和你的团队已经做了哪些前期工作?包括市场调研、咨询、原型开发等。(最多300字)

1.11 你计划何时做出雏型?或测试版?或可收费版本?(最多150字)请提供这3种版本的大概日期,如有必要请简要介绍。如果已经有雏型,请提供相关的链接。

1.12 是否已成立公司?: 否是,请提供公司信息(最多150字)

1.13 最大的担心是什么?可能遇到的风险和挑战是什么?(最多150字)

1.14 请列出三点你认为的,创新工场助跑计划可以最大帮助你的项目启动的方式。(最多150字)此问题无对错之分,我们只是想了解你对创新工场的期望和你最需要帮助的地方。

二、团队 (请填写创始人信息,至少有两个创始人。团队成员的背景和经历是我们评估的重要标准之一,请尽可能的详细填写之前有过的工作或项目经历。)
2.1 团队成员1
出生日期: 年 月 日
性别: 男女


时间段 毕业院校 专业 学位
自 年 月
到 年 月


时  间 年 月 日~ 年 月 日
职  位
出生日期: 年 月 日
性别: 男女


时间段 毕业院校 专业 学位
自 年 月
到 年 月


时  间 年 月 日~ 年 月 日
职  位

2. 2 团队成员互相认识多久了?团队是如何组建?(最多150字)

2.3 .团队中是否有人因各种原因不能在创新工场助跑计划及之后的创业中中全职参与项目?原因是什么?(最多150字)

2.4 请列出之前你最为自豪的个人项目或团队项目工作。(最多300字)

2.5 是否有以上团队成员以外的个人或组织参与过这个项目或帮助项目开发?(最多150字)


Older Posts »