-
-
Notifications
You must be signed in to change notification settings - Fork 251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added accessor for default primary key #110
Conversation
Can you please be more specific as to what problem this PR solves? Right now the model's primary key can be by default accessed as If you're using Why is it necessary to override the |
Sorry let me expand on the issue. The line that you referenced will allow you to get the name of the key but you can't actually get the value since it is set to private. This will cause an UnknownPropertyException. The line of code that you referenced will correctly get the key's id as '_id'; however, in the following lines of code the exception is thrown when your try to access $model['_id']. _id will first try to be resolved by the __get method in the BaseActiveRecord, when this happens it will call __get in the parent here. Once this is called the call will travel to the __get in the Component.php . Since the method does get exist in the class and it is not a behavior and exception will be thrown as getting an unknown property. Here is the stack trace to better explain the problem. yii\base\UnknownPropertyException: Getting unknown property: app\models\elastic\PlaceSearch::_id in /.../vendor/yiisoft/yii2/base/Component.php:143 |
Why are you trying to access the |
I'm not personally accessing/calling the _id attribute. I am constructing a new ActiveDataProvider and then calling the getModels method. The getModels then calls the prepare method, which in turn calls the prepareKeys method. Then the exception will be thrown up to the prepareKeys method when it tries to access _id here. |
Ok, I got it. Rather than updating |
@beowulfenator Okay good idea, I just updated my PR! |
Why is it failing the tests? |
I am trying to debug this locally; however, the test cases are failing for me on my branch and then also on the master branch in the same two places. Since it is happening on master also, I'm inclined to believe that it's unrelated to my changes. This is from the master branch...
|
public static function primaryKey() Put this function in your data model. When you set the id attribute value, it will map to the _id field of the elasticsearch |
@liyadongcn That does not map to the the _id field on each document in elastic. That will only work if you actually define an 'id' in the schema when creating templates for elastic and add it to the attributes. @beowulfenator I've looked at the issue further and if you add '_id' to the attributes on a model extending yii\elasticsearch\ActiveRecord, it will work. However, I feel that this should still work out of the box with the changes that we talked about. If you think that the solution should be to add '_id' to the list of attributes, I feel as though this should be added to the documentation. So here are fixes that makes it work:
|
@kyle-mccarthy For some strange reason the docs say:
Let me figure out why the tests are failing and just pull in this PR. |
Ok, so the tests are failing because tests for |
Thanks for your update, @kyle-mccarthy! |
@beowulfenator Thank you! Also after looking at open issues I believe this resolves #72 |
If a model extending the ActiveRecord class does not implement the primaryKey function, the primary key will default to '_id'. However, this property is inaccessible as it is private, resulting in an UnknownPropertyException. The accessibility of the property would need to be changed to protected, or an accessor method would need to be implemented (which I chose).
To reproduce the bug that this PR fixes: