Laravel 4.2自定义关系为复杂的多数据库查询添加Eager约束

时间:2015-09-23 15:39:47

标签: laravel laravel-4

我们正在建立一个系统,将教师与班级,部门,学校和定期调查,回应和安排联系起来。答案。

我们使用limesurvey作为调查组件。但是,教师界面包含在laravel代码库中。

在这个例子中,课程和他们的调查之间存在关系。这应该能够返回与该调查相关的任何调查的集合。

以下是我在CourseModel中所做的:

public function surveys()
{
    $related = new Survey();
    $parent = $this;
    return new CourseSurveysRelation($related->newQuery(), $this);
}

关系类:

使用Pivotal \ Cycle \ Models \ Cycle; 使用Pivotal \ Models \ CourseInterface; 使用Pivotal \ Survey \ Models \ Survey; 使用Pivotal \ Course \ Models \ Course;

使用Illuminate \ Auth \ UserInterface; 使用Illuminate \ Database \ Eloquent \ Builder; 使用Illuminate \ Database \ Eloquent \ Collection; 使用Illuminate \ Database \ Eloquent \ Relations \ Relation;

class CourseSurveysRelation extends Relation
{

    /**
     * Create a new relation instance.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @param  \Illuminate\Database\Eloquent\Model  $parent
     * @return void
     */
    public function __construct(Builder $query, CourseInterface $parent)
    {

        $survey_ids = $parent::getModel()
            ->newQuery()
            ->select('cycles_classes.limesurvey_id')
            ->join('cycles_classes', 'classes.id', '=', 'cycles_classes.class_id')
            ->where('cycles_classes.class_id','=',$parent->id)
            ->lists('limesurvey_id');

        $this->query = $query
            ->whereIn('sid', $survey_ids)
            ->groupBy('sid');

        $this->parent = $parent;
        $this->related = $query->getModel();
        $this->addConstraints();
    }


    public function addEagerConstraints(array $models)
    {
        $survey_ids = [];

        $course_ids = $this->getKeys($models, 'id');
        $survey_ids = \DB::table('cycles_classes')
                        ->select('cycles_classes.limesurvey_id')
                        ->whereIn('cycles_classes.class_id',$course_ids)
                        ->lists('limesurvey_id');

        $this->query->whereIn('sid', $survey_ids);
    }

    public function initRelation(array $models, $relation)
    {

    }

    public function addConstraints()
    {

    }

    public function match(array $models, Collection $results, $relation)
    {

    }

    public function getResults()
    {
        $results = $this->query->get();
        return $results;
    }

}

为了解释上面的内容,cycles_classes表保存了循环,类和调查ID。

以下是调查模型:

use \Config;
use Illuminate\Database\Eloquent\Model;
use Pivotal\Course\Models\Course;
use Pivotal\Cycle\Models\Cycle;
use Pivotal\Survey\Models\Collections\SurveyCollection;
use Pivotal\Survey\Models\Relations\SurveyCourseRelation;
use Pivotal\Survey\Models\Relations\SurveyCycleRelation;
use Pivotal\Survey\Models\Relations\SurveyResponseRelation;
use Pivotal\Survey\Models\SurveyInterface;

class Survey extends Model implements SurveyInterface
{
    protected $connection = 'limemysql';
    protected $table = 'surveys';
    protected $primaryKey = 'sid';

public function __construct($attributes = array())
{
    $this->table = Config::get('limesurvey.db_prefix').$this->table;
    parent::__construct($attributes);
}


public function questions()
{
    return $this->HasMany('Pivotal\Survey\Models\Question','sid','sid');
}

public function responses()
{
    $instance = new Response;
    $instance->setSid($this->sid);
    return new SurveyResponseRelation($instance->newQuery(),$this);
}

public function cycle()
{
    $instance = new Cycle();
    return new SurveyCycleRelation($instance->newQuery(),$this);
}

public function course()
{
    $instance = new Course();
    return new SurveyCourseRelation($instance->newQuery(),$this);
}

public function newCollection(array $models = [])
{
    return new SurveyCollection($models);
}

}

当我没有对模型调用进行任何更复杂的修改时,代码似乎正确并且有效,但是例如,当我调用时: $父 - >在(' courses.surveys') - >得到() 我得到以下与sql相关的错误:

SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在(?,?,?,?,?,?,?,?,?,?, ?,?,?,?,?,?,?,?,?,?,?,?'在第1行(SQL:select * from sid其中surveys in()和sid in(531557,211231,316692,899214,974717,247825,697782,552663,345392,818373,628794,934974,181774,981825,768386,556866,867981,454781,997136,681294,219854, 536314,533283,661282,419684,643469,893499,951161,766562,238263,289637,975286,149335,179627,932135,763659,219722,214899,144484,381495,462774,949323,918832,414629,646126,342421, 365583,286479,386494,177941,693328,634294,434532,721241,881355,425659,971835,997579,653716,775787,437953,758524,931996,557856,179929,251354,2362881,565872,898672,862168,264461, 383562,855941,351452,734981,523667,616126,723435,282423,275988,549494,644412,112726,876769,637444,233532,276116,953935,353756,912427,862134,996477,333321,3 22652,917295,842584,123751,628385,871618,417254,352626,523421,217281,532171,)分组sid

似乎我需要摆脱$ this-> query-> whereIn(' sid',$ survey_ids)的第一个实例;

我在这里缺少什么?

0 个答案:

没有答案