承接 chinpei215/cakephp-stackable-finder 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

chinpei215/cakephp-stackable-finder

Composer 安装命令:

composer require chinpei215/cakephp-stackable-finder

包简介

CakePHP 2.x plugin to make it possible to stack custom finders

README 文档

README

Software License Build Status Coverage Status

StackableFinder Plugin for CakePHP 2.x

Requirements

  • CakePHP 2.x
  • PHP 5.3+

Installation

  • Put StackableFinder directory into your plugin directory. You can also install via Composer.
  • Enable StackableFinder plugin in your app/Config/bootstrap.php file.
  • Enable StackableFinder.StackableFinder behavior in your model.

Usage

$articles = $this->Article->q()
	->find('all', ['conditions' => ['Article.created >=' => '2015-01-01']])
	->findAllByUserId(1) // Magic finder
	->find('published') // Custom finder
	->find('list')
	->exec();

You can start stacking finders by calling q(), and you can execute the query and get the resutls by calling exec().

Note that q() method returns an instance of StackableFinder. The object also has find() method like a Model - so you can use fluent interface, but it is not a sub-class of Model.

So you cannot call any other methods implemented by Model.

$this->Article->q()->read(); // Error 

Instead, you can use where() or some other 3.x compatible methods for building queries.

$articles = $this->Article->q()
	->select(['Article.id', 'Article.title'])
	->contain('Author')
	->where(['Article.user_id' => 1])
	->order(['Article.id' => 'DESC'])
	->limit(10)
	->offset(0)
	->exec();

Subqueries

You can make subqueries like the following:

$q = $this->User->q()->select('id');

$articles = $this->Article->q()
	->where([
		'user_id IN ?' => [$q]
	])
	->exec();

You will see that IN ? appears after the field name in the left-hand side, and you will see also that $q appears inside an [] in the right-hand side.

It is not compatible with 3.x but it is nessecary at this time in 2.x.

Getting results

As mentioned above, you can do it by calling exec() but there are some other ways to get the results of the query.

You can iterate the StackableFinder object directly.

$q = $this->Article->q()->find('list');
foreach ($q as $id => $title) {
	// ...
}

Or you can use first() or count() instead of exec().

$articles = $this->Article->q()
	->find('published')
	->first();

This is same as the following:

$articles = $this->Article->q()
	->find('published')
	->find('first')
	->exec();

But, note that stacking find('first') or first() after find('list') doesn't work. Because _findFirst() doen't returns the first result actually. That returns the element with index 0.

So this is a bad example:

$articles = $this->Article->q()
	->find('list')
	->first();

You will get an empty array instead of the first item of the list.

Also note that stacking find('count') or count() after find('list') doesn't work. Because _findCount() expects an array like [['Model' => ['count' => N ]]], but _findList changes the array before it get called.

You can override these methods in your model to change the behaviors, if necessary.

统计信息

  • 总下载量: 29
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 10
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-12-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固