Phalcon Tools model create alias is duplicated for FK to same table - phalcon

I am trying to scaffold a model using Phalcon Tools. Problem is that I have various FK's that reference the same table, for instance: user_created and user_modified in User table.
The model then aliases them both as follows:
$this->hasMany('id', 'PhalconRest\Models\User', 'created_by', ['alias' => 'User']);
$this->hasMany('id', 'PhalconRest\Models\User', 'modified_by', ['alias' => 'User']);
Is there a way to override this behaviour? I cannot seem to find a switch for that.

You can just change the Alias
https://docs.phalconphp.com/en/3.1/db-models-relationships#aliases
$this->hasMany('id', 'PhalconRest\Models\User', 'created_by', ['alias' => 'User1']);
$this->hasMany('id', 'PhalconRest\Models\User', 'modified_by', ['alias' => 'User2']);

Related

Search related data in different data base

I use yii2 to build one app which need to connect some tables. I can join them simply and search for data in related fields. I am doing it by adding the connection like this>
public function getNextTab()
{
return $this->hasOne(NextTab::className(),['id' =>'id_nexttab']);
}
and ask for the data in search model using like this ->
->where ('id'='ok') ->
->joinWith('nextTab')
->joinWith('nextTab.nextTab1')
->joinWith('nextTab.nextTab1.nextTab2');
My problem is when I try to do this with tables from different database. The query is give me error like
SQLSTATE[42S02]: Base table or view not found:
any tips how to pass it? or how to do other way of connection to have the data.
Joining tables from different databases may not be supported by your RDBMS (PostgreSQL for example). But if supported (MSSQL, MySQL) then table names should be prefixed with database name (and schema if needed). You can achieve this in Yii2 using {{%TableName}} syntax in tableName() function.
public static function tableName()
{
return '{{%table_name}}';
}
But be careful with joining tables from different databases if they are located on different servers -- this can be very slow.
If you just want to get related data (joined tables are not used in WHERE) then use with() instead of joinWith(). This will be executed as separate query with IN statement. In most cases this way has a better performance and no problems with different sources (and even different DBMS).
->with('nextTab', 'nextTab.nextTab1', 'nextTab.nextTab1.nextTab2')
Configure your second database component in the application's config.
Override the getDB() function in your ActiveRecord Model to return the second DB component.
This will attach your Model to the secondary DB and allow you to query from the table in secondary DB.
Config sample:
'components' => [
'db2' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=db2',
'username' => 'hello',
'password' => 'world',
],
],
getDB() function override sample:
class Tab extends ActiveRecord
{
public static function getDb()
{
// use the "db2" component
return \Yii::$app->db2;
}
}
Good Luck!

Authorization::Roles doesn't work in Catalyst app

I've tried use Authorization::Roles in my Catalyst app. Authentication works right but when I use $c->check_user_roles("admin"); it always returns false. What's wrong?
Some config:
store => {
class => 'DBIx::Class',
user_model => 'Mymodel::User',
id_field => 'name',
role_field => 'rolename', # in my table there is the same column
# name undoubtedly
}
If your roles are stored in the users table, use the role_column option. If your roles are stored in a separate table, use the role_relation and role_field options. role_field on its own isn't valid.

Cakephp, i18n, SQL Error, Not unique table/alias

I get the following SQL error:
SQL Error: 1066: Not unique table/alias: 'I18n__name'
when doing a simple find query.
Any ideas on possible situations that may have caused this??
I'm using a bindModel method to retrieve the data is that related?
This is my code:
$this->Project->bindModel(array(
'hasOne' => array(
'ProjectsCategories',
'FilterCategory' => array(
'className' => 'Category',
'foreignKey' => false,
'conditions' => array('FilterCategory.id = ProjectsCategories.category_id')
))));
$prlist = $this->Project->find('all', array(
'fields' => array('DISTINCT slug','name'),
'conditions' => array('FilterCategory.slug !='=>'uncategorised')
))
You have not used or initialized the required table/model you are using in your controller. Use var $uses = array('your_table_name');
I do not have a direct answer to my problem. However after some research I came to the following conclusion.
I've decided to change my methodology and stop using the translate behavior in cakephp.
I've found another behavior called i18n which works much better when dealing with associated models. You can read about it http://www.palivoda.eu/2008/04/i18n-in-cakephp-12-database-content-translation-part-2/#comment-1380
In cakephp book it says :
"Note that only fields of the model
you are directly doing find on will
be translated. Models attached via
associations won't be translated
because triggering callbacks on
associated models is currently not
supported."
I'm not sure if I made the right choice however I have been struggling to get the translate behavior to work in cakephp and this solution at least makes the project I'm working functional.

Fluent NHibnernate HasManyToMany with Index

I'm trying to map a many-to-many collection with Fluent NHibnernate. My model class has this property:
public virtual IList<Resource> Screenshots
{
get { return _screenshots; }
protected set { _screenshots = value; }
}
And my fluent mapping is:
HasManyToMany(x => x.Screenshots)
.AsList(x => x.WithColumn("Index"))
.Cascade.AllDeleteOrphan();
When I run my application, I get the following exception message:
The element 'list' in namespace
'urn:nhibernate-mapping-2.2' has
invalid child element 'many-to-many'
in namespace
'urn:nhibernate-mapping-2.2'. List of
possible elements expected: 'index,
list-index' in namespace
'urn:nhibernate-mapping-2.2'.
There should be a way to do this. Does anyone know what I am doing wrong?
The current FluentNHibernate syntax for this is as follows:
HasManyToMany(x => x.Screenshots)
.AsList(i => i.Column("`Index`"));
The index column defaults to Index, but that's a reserved word on SQL Server (and probably other databases too), so you must quote it with back-ticks.
Also, I'd recommend against setting a cascade on this relationship. Consider the following code:
x.Screenshots.Remove(s);
session.SaveOrUpdate(x);
NHibernate will correctly delete rows from the linking table even without a cascade specified. However, if you specify AllDeleteOrphan, then NHibernate will delete the row from the linking table and also delete Resource s. I doubt this is the behavior you want on a many-to-many relationship.

Fluent Nhibernate composed entity, specify parent key

In this question I was answered hot to map a composed entity from the primary key of the table.
So given:
public UserMap()
{
WithTable("aspnet_Users");
Id(x => x.Id, "UserId")
.GeneratedBy.Guid();
Map(x => x.Name, "UserName");
Map(x => x.Login, "LoweredUserName");
WithTable("LdapUsers", m => {
m.Map(x => x.FullName, "FullName");
m.WithKeyColumn("UserId");
});
}
everithing works if in the "LdapUser" and in the "aspnet_Users" there is a column named "UserId".
What If I want to specify both the colum name for the foreign key table and the column name for the key from the main table as this is not the pk so use another column to do the join?
It looks like this is not currently supported at all in NHibernate. There is an open issue for it on NHibernate's JIRA. Actually, I think this is for collections but probably applies for what you're doing as well. The idea is you would add a property-ref attribute on your <key> element that points to the property you want to reference instead of the primary key.
Supposedly the change is already available in the Alpha2 release of NHibernate 2.1.0. Of course, it might be sometime after NH2.1 is a general release before this gets added to FNH. If you can, I would recommend getting a hold of the Alpha2 release and trying it with your situation. If it doesn't work for your composed entity, only collections, you might want to submit another issue on JIRA so it gets added as well.