2016-05-21 24 views
13

3.3 Ben CakePHP'de Mevcut tabloda sütun eklemek istediğiniz

Benim ContactsTable.php dosya kodu: Ben anlatıldığı gibi çalıştık

<?php 
namespace App\Model\Table; 
use Cake\ORM\Table; 
use Migrations\AbstractMigration; 

class ContactsTable extends Table 
{ 
    public function initialize(array $config) 
    { 
     $this->addBehavior('Timestamp'); 
     $table = $this->table('contacts'); 
     $table->addColumn('price', 'decimal')->update(); 

    } 
} 

Call to a member function addColumn() on a non-object

nasıl on-the-fly kontrolör üzerinden sütunları eklerim: ama CakePHP 3 belgelerinde bu hata var?

+0

$ table-> schema() -> addColumn ('price', 'decimal') -> update(); '? Sadece bir tahmin, cakephp içinde göç hakkında çok şey bilmiyorum – arilia

+0

@arilia Bir belge linki ile başvuruda bulunabilir misiniz? Bence yakınsın. – Karma

cevap

6

:

<?php 

namespace App\Controller; 

use Cake\Core\Configure; 
use Cake\Network\Exception\NotFoundException; 
use Cake\View\Exception\MissingTemplateException; 
use Cake\ORM\TableRegistry; 
use Cake\Database\Schema\Table; 
use Cake\Datasource\ConnectionManager; 
use \Migrations\AbstractMigration as AbstractMigration; 
use \Phinx\Db\Adapter\MysqlAdapter as MysqlAdapter; 

class PagesController extends AppController 
{ 
    public function display() 
    { 
     $connectionArray = ConnectionManager::get('default')->config(); 
     $connectionArray['pass'] = $connectionArray['password']; 
     $connectionArray['user'] = $connectionArray['username']; 
     $connectionArray['name'] = $connectionArray['database']; 

     $migrationObject = new AbstractMigration(mt_rand()); 
     $migrationObject->setAdapter(new MysqlAdapter($connectionArray)); 
     $tree = $migrationObject->table('tests'); 


     $tree->addColumn('something', 'text') 
         ->update(); 
    } 
} 

Hacking kaç saat sonra, nihayet on-the-fly bunu yapmak için bir yol buldu.

farklı bir veritabanı adaptör kullanıyorsanız, MysqlAdapter o adapater olarak değiştirin

varsayılan CakePHP'de 3 (- - bugün itibariyle 2. Haziran '16 son) 'de test edilmiştir.

Note to the users:

  • This is an ugly hack and should be used ONLY if you do not work in an organization where each migration commit requires peer reference.

  • mt_rand() must NEVER be used as a version number hack.

  • There is no canonical way of doing it via the controllers. Update in a datasource MUST always be done modified via migrations - using a proper structure.

  • Refer to Running Migrations in a non-shell environment and try to create a migrations logs under /config/migrations , that would be more rule-specific-on-the-fly and you will also have logs for peers to review.

1

Eğer ürün masanın örneğin 'fiyat' için yeni bir sütun eklemek istediğiniz ve fiyatı bir 'ondalık' projenize gidip konsolda bu yazmalıdır ise:

bin/cake bake migration AddPriceToProducts price:decimal 

Sen yeni bir dosya örn görebilirsiniz Yapılandırma/Taşıma/20160501190410_AddPriceToProducts.php

<?php 
use Migrations\AbstractMigration; 

class AddPriceToProducts extends AbstractMigration 
{ 
    /** 
    * Change Method. 
    * 
    * More information on this method is available here: 
    * http://docs.phinx.org/en/latest/migrations.html#the-change-method 
    * @return void 
    */ 
    public function change() 
    { 
     $table = $this->table('products'); 
     $table->addColumn('price', 'decimal', [ 
      'default' => null, 
      ... 
      'null' => true, 
     ]); 
     $table->update(); 
    } 
} 

ve daha sonra sadece konsolda bunu yazmak, veri tabanı için bu sütun eklemek geçişleri başlatmak:

bin/cake migrations migrate 
+1

Teşekkürler Jacek B Budzyñski. Sistemimde, kullanıcı istediği gibi dinamik olarak sütun oluşturabilir. Dolayısıyla, kullanıcı birden fazla sütun eklemek istediğinde, bu konsol kodunu yapamaz. Taşıma işlemini başlatan ve ardından bu konsol öğeleri olmadan sütun ekleyen herhangi bir kod var mı? –

+1

@JigarDhaduk Birden fazla sütun ekleyebilirsiniz 'bin/kek fırında göç AddPriceAndDiscountAndSomeToProducts fiyat: ondalık indirim: tamsayı biraz: string' ama konsol olmadan nasıl yapılacağını bilmiyorum .. üzgünüm –

+0

Soruyu düzeltin. @ JacekBBudzyñski, OP, uçakta nasıl yapılacağını sormak istiyor. – Karma

1

Göç eklenti ayrıca Running Migrations in a non-shell environment desteklemektedir.

Since the release of version 1.2 of the migrations plugin, you can run migrations from a non-shell environment, directly from an app, by using the new Migrations class. This can be handy in case you are developing a plugin installer for a CMS for instance. The Migrations class allows you to run the following commands from the migrations shell: migrate , rollback , markMigrated , status and seed .

Each of these commands has a method defined in the Migrations class.

Kullanıcı tarafından sütun verilerini kabul edecek ve geçişi çalıştıracak bazı özel işleyicileri hazırlayabilirsiniz. Bu durumda name ve type girişleri ile bir form olabilir. Veri form verildikten sonra DB'ye geçiş uygulanacaktır. İşte

nasıl kullanılacağını geçerli: Kod

use Migrations\Migrations; 

$migrations = new Migrations(); 

// Will return an array of all migrations and their status 
$status = $migrations->status(); 

// Will return true if success. If an error occurred, an exception will be thrown 
$migrate = $migrations->migrate(); 

// Will return true if success. If an error occurred, an exception will be thrown 
$rollback = $migrations->rollback(); 

// Will return true if success. If an error occurred, an exception will be thrown 
$markMigrated = $migrations->markMigrated(20150804222900); 

// Will return true if success. If an error occurred, an exception will be thrown 
$seeded = $migrations->seed(); 
+0

$ tsg Cevabınız için teşekkür ederiz. Ancak bu kodla yeni sütunları nasıl ekleyebilirim? Herhangi bir örnek verir misiniz lütfen? –

+1

Bu, basitçe içeriğin bir kopyala yapıştırmasıdır – Karma

İlgili konular