B. Search

Inside module/post make a new file called search.php and paste Figure 7.

Figure 7. Post Search
<?php //-->
class Post_Search extends Eden_Sql_Search {
	/* Constants
	/* Public Properties
	/* Protected Properties
	protected $_collection	= 'Post_Collection';
	protected $_model		= 'Post_Model';
	protected $_table		= 'post';
	/* Private Properties
	/* Magic
	public function __construct() {
		$this->_database = Eden::i()->getActiveApp()->getDatabase();
	/* Public Methods
	public function filterByUserName($name) {
		return $this
			->innerJoinOn('user', 'user_id=post_user')
			->addFilter('user_name=%s', $name);
	/* Protected Methods
	/* Private Methods

In the figure above we extend Eden_Sql_Search and added the method filterByUserName() which is an example of why we may want to extend Eden_Sql_Search in the first place. This method simplifies a common routine into a method call. Figure 8 shows how we can go about using the search class immediately.

Figure 8. Searching

In the figure above there's a few methods being powered with magic, but we'll just start going down the line. First off, innerJoinOn() from Figure 7 is the new way we accept joins. There are 8 methods dedicated to different kinds of joins.

Kinds of Join methods
  • innerJoinOn()
  • innerJoinUsing()
  • leftJoinOn()
  • leftJoinUsing()
  • rightJoinOn()
  • rightJoinUsing()
  • outerJoinOn()
  • outerJoinUsing()

No matter what methods you choose from above there are 2 arguments you need to add. The first argument is the name of the table you would like to join and the second one is the how they relate to each other.

The first magic powered method is called filterByPostActive(). There is no actual method called filterByPostActive() in the SQL class. Instead when this function is called it will parse out the name of the method and recognize that PostActive is the name of a column and convert that into addFilter('post_active=%s', 1).

addFilter() generally accepts 2 arguments. The first argument is the filter clause. If you notice in our filter example in the above paragraph we use %s to delimit a binded value. You can have as many binded values per filter as you like. The following arguments need to include the binded values in order of when they occur in the filter clause. For example: addFilter('post_active=%s OR post_active=%s', 1, 2).

The second magic powered mehod is called sortByUserId('ASC').There is no actual method called sortByUserId('ASC') in the MySQL class. Instead when this function is called it will parse out the name of the method and recognize that UserId is the name of a column and convert that into addSort('user_id', 'ASC').

There are 3 kinds of pagination methods also available

Pagination Methods
  • setRange(75)
  • setStart(25)
  • setPage(1)

It's important if you are going to use setPage(1) to call setRange(75) first because the underlying function simply calculates the start index based on the range. Two other methods that are not covered by Figure 8 are the ability to group and to set the table to something else.

Figure 9. Other Useful methods
Getting Results

When your happy with your query you can retrieve the results in 3 ways as described in Figure 10.

Figure 10. Retrieving Results

Figure 10 shows 3 ways to get the results, the first way getTotal(), will retrieve the total number and does not consider pagination elements. getRows() will simply return a raw array. getCollection() will return you an object with the results for further manipulation. We will cover collections later in this section.

© 2012 Openovate Labs. All rights reserved.