I have a problem with setting up entities mapping in silex app.
Registering services:
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'db.options' => array(
'driver' => 'pdo_mysql',
'dbname' => 'blabla',
'host' => 'blabla',
'user' => 'blabla',
'password' => 'blabla',
'port' => '3306',
'charset' => 'utf8'
),
));
$app->register(new DoctrineOrmServiceProvider(), [
'orm.em.options.mappings' => [
[
'type' => 'simple_yml',
'namespace' => 'App\Entities',
'path' => __DIR__. '/Resources/orm/mappings/',
],
]
]);
src/Resources/orm/mappings/User.orm.yml:
User:
type: entity
table: users
id:
id:
type: integer
generator:
strategy: AUTO
fields:
username:
type: string
email:
type: string
github_token:
type: string
github_token_created_at:
type: datetimetz
created_at:
type: datetimetz
updated_at:
type: datetimetz
And entity class itself is in: src/Entities/User.php
cli-config.php file:
<?php
require_once 'vendor/autoload.php';
$app = require_once 'src/app.php';
return \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($app['orm.em']);
And I am running following command:
vendor/bin/doctrine orm:info
I am getting info about lack of mappings:
[Exception]
You do not have any mapped Doctrine ORM entities according to the current configuration. If you have entities or mapping files you should check your mapping configuration for errors`
I am 99% sure paths are set up ok, the connection to database is correct, I have no idea why it doesn't work. Could someone help me with this?
Most mappings i've seen are like this
'mappings' => array(
// Using actual filesystem paths
array(
'type' => 'annotation',
'namespace' => 'Foo\Entities',
'path' => __DIR__.'/src/Foo/Entities',
),
You do not seem to have your namespace and path in any notable relationship
Related
I have a requirement to configure ownership for priceList entities. To approach this I created a migration to add the required fields:
$this->extendExtension->addManyToOneRelation(
$schema,
$table,
'organization',
$organizationTable,
'name',
[
'extend' => [
'is_extend' => true,
'owner' => ExtendScope::OWNER_SYSTEM,
'without_default' => true,
]
]);
$this->extendExtension->addManyToOneRelation(
$schema,
$table,
'owner',
$businessTable,
'name',
[
'extend' => [
'is_extend' => true,
'owner' => ExtendScope::OWNER_SYSTEM,
'without_default' => true,
]
]
);
Then updated entity configuration information with:
$params = [
"owner_type" => "BUSINESS_UNIT",
"owner_field_name" => "owner",
"owner_column_name" => "owner_id",
"organization_field_name" => "organization",
"organization_column_name" => "organization_id"
];
foreach($params as $code => $value) {
$queries->addPostQuery(
new UpdateEntityConfigEntityValueQuery(
PriceList::class,
'ownership',
$code,
$value
)
);
}
Migration processed without issues but for the data grid on the priceList index page error occurred.
An exception occurred while executing 'SELECT count(o0_.id) AS sclr_0 FROM oro_price_list o0_ WHERE o0_. = 1'
It looks like the data grid couldn't reach the organization name to handle the pagination query. Data grid unmodified grid from ORO 4.1 EE
Everything works fine if ownership configuration updated via SetOwnershipTypeQuery intead of UpdateEntityConfigEntityValueQuery
$queries->addQuery(
new SetOwnershipTypeQuery(
PriceList::class,
[
'owner_type' => 'BUSINESS_UNIT',
'owner_field_name' => 'owner',
'owner_column_name' => 'owner_id',
'organization_field_name' => 'organization',
'organization_column_name' => 'organization_id'
]
)
);
Can you help me. How to relate it and make a join
. i get error has no relation named "project".
i using ActiveRecord with my code :
$posts = MaKantor::find()
->leftJoin('project.track', '`track`.`id_office` = `m_kantor`.`kantor_id`')
->with('project.track')->where(['collecting_id' => $model->collecting_id])
->all();
and config
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=project',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
'db2' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=db_master',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
When you use with('relationName') in query, relation function needs to be defined in MaKantor model.
For example :
public function getProject()
{
return $this->hasOne(Project::className(), ['id' => 'project_id']);
}
I'm developing a API in YII2 with multiple databases. I want to choose the database in real time. The idea is to read one variable available in controler (API key) to identify the correct connection database in model.
For example in webapplication like a portal i have (it works):
in db default connection i have
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=dbcompany_0',
'username' => 'root',
'password' => 'XXXXXXXXXXXXXXXXXXXX',
'charset' => 'utf8',
],
In model I have
public function tableName()
{
$schema = '';
$user = User::find(Yii::app()->user->id);
$schema = "dbcompany_". $user->CompanyId;
return $schema . '.' . 'customer';
}
With this approach I just have a single db connection for all database companies.
How I can apply the same\similiar approach in API. I don't have sessions.
Any idea is well welcome.
Create a new component under db as:
'db1' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=dbcompany_0',
'username' => 'root',
'password' => 'XXXXXXXXXXXXXXXXXXXX',
'charset' => 'utf8',
],
'db2' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=dbcompany_0',
'username' => 'root',
'password' => 'XXXXXXXXXXXXXXXXXXXX',
'charset' => 'utf8',
],
And use it as:
Yii::$app->db1
Yii::$app->db2;
All requests are authenticate using a token and this is associated to the user. When the authentication is done YII Framework assigns the userid to \Yii::$app->user->identity->id. So from anywhere I can identify the company owner in model.
public static function tableName(){
$moreinfo= account\Userextra::findOne(\Yii::$app->user->identity->id);
$schema = \Yii::$app->params['table.prefix'] . $moreinfo->CompanyId;
return $schema . '.country';
}
It was successfully working before I changed the authentication service name from 'orm_default' to 'admin', and it is necessary since I have more modules which uses more authentication services.
The problem is I'm getting the following error:
Fatal error: Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for doctrine.authenticationservice.admin'
My module.config.php
'doctrine' => array
(
'driver' => array
(
__NAMESPACE__ . '_driver' => array
(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
),
'orm_default' => array
(
'drivers' => array
(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
),
)
),
'authentication' => array
(
'admin' => array
(
'object_manager' => 'Doctrine\ORM\EntityManager',
'identity_class' => __NAMESPACE__ . '\Entity\User',
'identity_property' => 'email',
'credential_property' => 'password',
'credentialCallable' => __NAMESPACE__ . '\Model\User::hashPassword'
),
),
),
My Module.php
public function getServiceConfig()
{
return array
(
'factories' => array
(
'Admin\Auth' => function($sm)
{
return $sm->get('doctrine.authenticationservice.admin');
},
'Admin\Form\Auth\Login' => function($sm)
{
return new Form\Auth\Login();
},
),
);
}
It was confirmed as a bug: https://github.com/doctrine/DoctrineORMModule/issues/291
According to a comment in a doctrine module source file it plans to be fixed in 1.0. In this meantime you can it in your any module.config.php file of your application like this:
'authentication' =>
[
'application' =>
[
'object_manager' => 'Doctrine\ORM\EntityManager',
'identity_class' => 'Application\Entity\Customer',
'identity_property' => 'email',
'credential_property' => 'password',
'credentialCallable' => 'Application\Entity\Customer::hashPassword'
],
],
'authenticationadapter' =>
[
'application' => true,
],
'authenticationstorage' =>
[
'application' => true,
],
'authenticationservice' =>
[
'application' => true,
]
I had exactly same problem when i was working on my project. After working my *ss out for two nights, i solved the problem by simply re-installing the doctrine-orm-module after reading https://github.com/doctrine/DoctrineORMModule, "Registered Service names" section. This simply means doctrine orm module wasn't properly installed, or wasn't installed.
I need create a SaaS application structure in YII Framework using separate database, but the examples in the web are using a single database. Any information or handbook will be welcome.
Thanks
You can use a second DB by adding it to your main.php ex:
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=database',
'emulatePrepare' => true,
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'schemaCachingDuration' => 3600,
'enableProfiling' => true,
),
'db2'=>array(
'connectionString' => 'mysql:host=localhost;dbname=database2',
'class'=>'CDbConnection',
'emulatePrepare' => true,
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'schemaCachingDuration' => 3600,
'enableProfiling' => true,
),
Then you can set the getDbConnection() method inside the active records that are to use this second db. I would suggest making a parent class extending from CActiveRecord with this code and then extending that.
It should contain:
public static $db2;
public function getDbConnection()
{
if(self::$db2!==null)
return self::$db2;
else
{
self::$db2=Yii::app()->db2;
if(self::$db2 instanceof CDbConnection)
{
self::$db2->setActive(true);
return self::$db2;
}
else
throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.'));
}
}