Esempio n. 1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Artist::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'user_id' => $this->user_id]);
     //        $query->andFilterWhere(['like', 'original_name', $this->original_name])
     //            ->andFilterWhere(['like', 'latin_name', $this->latin_name]);
     $query->andWhere('original_name LIKE :original_name OR latin_name LIKE :latin_name')->addParams([':original_name' => "%{$this->original_name}%", ':latin_name' => "%{$this->latin_name}%"]);
     if (Utils::get('tags', $params)) {
         $query->leftJoin('artist_tag', 'artist_tag.artist_id = artist.id');
         if (Utils::get('tags_mode', $params)) {
             # all tags
             $tags = explode(',', $params['tags']);
             foreach ($tags as $k => $v) {
                 $tags[$k] = (int) $v;
             }
             $tags_count = count($tags);
             $tags = implode(',', $tags);
             $query->andWhere("(\n                    SELECT count(*)\n                    FROM artist_tag\n                    WHERE artist_tag.artist_id = artist.id\n                    AND artist_tag.tag_id IN ({$tags})\n                ) = {$tags_count}");
         } else {
             # any tag
             $query->andWhere(['in', 'artist_tag.tag_id', explode(',', $params['tags'])]);
         }
     }
     return $dataProvider;
 }
 /**
  * Create the "Various Artists".
  *
  * @return void
  */
 public function up()
 {
     Artist::unguard();
     $existingArtist = Artist::find(Artist::VARIOUS_ID);
     if ($existingArtist) {
         if ($existingArtist->name === Artist::VARIOUS_NAME) {
             goto ret;
         }
         // There's an existing artist with that special ID, but it's not our Various Artist
         // We move it to the end of the table.
         $latestArtist = Artist::orderBy('id', 'DESC')->first();
         $existingArtist->id = $latestArtist->id + 1;
         $existingArtist->save();
     }
     Artist::create(['id' => Artist::VARIOUS_ID, 'name' => Artist::VARIOUS_NAME]);
     ret:
     Artist::reguard();
 }
 /**
  * Create the "Various Artists".
  *
  * @return void
  */
 public function up()
 {
     // Make sure modified artists cascade the album's artist_id field.
     Schema::table('albums', function ($table) {
         $table->dropForeign('albums_artist_id_foreign');
         $table->foreign('artist_id')->references('id')->on('artists')->onUpdate('cascade')->onDelete('cascade');
     });
     Artist::unguard();
     $existingArtist = Artist::find(Artist::VARIOUS_ID);
     if ($existingArtist) {
         if ($existingArtist->name === Artist::VARIOUS_NAME) {
             goto ret;
         }
         // There's an existing artist with that special ID, but it's not our Various Artist
         // We move it to the end of the table.
         $latestArtist = Artist::orderBy('id', 'DESC')->first();
         $existingArtist->id = $latestArtist->id + 1;
         $existingArtist->save();
     }
     Artist::create(['id' => Artist::VARIOUS_ID, 'name' => Artist::VARIOUS_NAME]);
     ret:
     Artist::reguard();
 }