fuzzyma / contao-eloquent-bundle
为您的dca文件创建使用eloquents关系的可能性
0.1.2
2016-08-10 15:24 UTC
Requires
- php: ^5.5 | ^7.0
- contao/core-bundle: ^4.2
- symfony/console: ^3.1
- symfony/filesystem: ^3.1
- symfony/http-kernel: ^3.1
- wouterj/eloquent-bundle: ^0.2.3
This package is not auto-updated.
Last update: 2024-09-14 19:09:25 UTC
README
此包依赖于wouterj的杰出作品eloquent-bundle,这使得eloquent可以在symfony中使用。我们想更进一步,将Eloquent集成到Contao中。
目前此包实现了以下功能
- hasMany, hasOne, belongsTo, belongsToMany关系
- 用于创建分表dca文件的命令
- 您可以在模型中使用的几个特质
- ContaoModelConsumerTrait(将contao模型转换为eloquent模型)
- 所有核心Contao模型的eloquent模型
更多即将实现的功能
- hasManyThrough
- morphTo
- morphMany
- morphToMany
- morphedByMany
指南
1. 安装包
composer require fuzzyma/contao-eloquent-bundle
2. 激活包
在AppKernel.php中添加以下内容
public function registerBundles() { $bundles = [ //... new WouterJ\EloquentBundle\WouterJEloquentBundle(), // we depend on it so we have to load it new Fuzzyma\Contao\EloquentBundle\ContaoEloquentBundle() // and that's ours ]; }
现在这些包已经激活。但是,我们必须配置eloquent环境。您可以在eloquent-bundle中看到它的工作原理。但是,为了简化,这里有一个简短版本
3. 配置Eloquent
wouterj_eloquent: connections: default: database: "%database_name%" driver: mysql host: "%database_host%" username: "%database_user%" password: "%database_password%" charset: utf8 collation: utf8_unicode_ci prefix: 'tl_' default_connection: default eloquent: true aliases: false
完成此操作后,我们最终可以使用它
用法
假设我们有一个Topic
模型和一个Tag
模型。每个Topic
可以拥有多个Tag
,反之亦然。为了使用Eloquents关系,我们需要一个分表,但contao不理解分表。此外,我们希望有一个用于主题和标签的多选字段,它工作得很好。我们可以这样做
/** * Table tl_topics */ $GLOBALS['TL_DCA']['tl_topics'] = array ( 'config' => array ( // all the config 'model' => Fuzzyma\Contao\EloquentExampleProjectBundle\Models\Topic::class ) // Fields 'fields' => array ( // id, tstamp and so on and: 'tags' => array ( 'label' => &$GLOBALS['TL_LANG']['tl_topics']['tags'], 'inputType' => 'select', 'eval' => ['multiple' => true], 'eloquent' => [ 'relation' => 'belongsToMany', 'model' => Fuzzyma\Contao\EloquentExampleProjectBundle\Models\Tag::class, 'label' => '%name% [%comment%]' // 'medthod' => 'tags' // is automatically assumed ] ), ) );
重要部分是配置部分中的模型键和在tags部分中的eloquent键。注意:请确保字段名与模型中定义的相同。因此,您的Topic模型将如下所示
namespace NamespaceToBundle\Models; use Illuminate\Database\Eloquent\Model; use Fuzzyma\Contao\EloquentBundle\Traits\ContaoFilesModelTrait; // only needed if you deal with files class Topic extends Model { use ContaoFilesModelTrait; // laravel use plural of class name as table. So in case you don't match the convention you can change it here protected $table = "topics"; protected $guarded = []; public $timestamps = false; // we don't have timestamps but we could add them if we want // that method name must match the field name (without _id) or the specified method!! public function tags(){ return $this->belongsToMany(Tag::class, 'tag_topic'); // you may want to name the pivot table here } // here the ContaoFilesModelTrait comes into place. // The Mutator will convert a call to $topic->thumbnail to a contao file model // of course that only works if you defined a field thumbnail in the dca public function getThumbnailAttribute($uuid){ return $this->getContaoFile($uuid); } // same for setting a new file. Pass a FileModel, an UUID string or just binary public function setThumbnailAttribute($file){ return $this->setContaoFile($file); } }
有关更多代码和其他关系的详细信息,请参阅contao-eloquent-example-project-bundle
生成器
此包附带一个用于为您创建dca文件的命令,然后创建分表。只需运行以下命令
php app/console contao:make:pivot
有关更多选项,请运行
php app/console contao:make:pivot --help