Skip to content
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

Provide a simple way to test that a field exists in a model #323

Closed
wants to merge 4 commits into from

Conversation

firemankurt
Copy link
Contributor

When creating a behavior class where most models might have for example, a user_id field, it is very helpful to find out if the current $model you are working with has that field so you can fill it or process it in some cases.

For example:
CompanyBehavior.php

protected function createModel() {
	$this->model = new $this->modelClass;
	// set company of new model
	$this->model->company_id = $this->user()->company_id;

	if ($this->hasAttribute('user_id')) {
		$this->model->user_id = $this->user()->id;
	}
	return $this->model;
}  

This is a resubmital of: #321

When creating a behavior class where most models might have for example, a user_id field, it is very helpful to find out if the current $model you are working with has that field so you can fill it or process it in some cases.

For example:
CompanyBehavior.php
             
    protected function createModel() {
		$this->model = new $this->modelClass;
		// set company of new model
		$this->model->company_id = $this->user()->company_id;

		if ($this->hasAttribute('user_id')) {
			$this->model->user_id = $this->user()->id;
		}
		return $this->model;
	}  

This is a resubmital of: octobercms#321
@firemankurt firemankurt mentioned this pull request May 25, 2018
src/Database/Model.php Outdated Show resolved Hide resolved
…hod variable names.

Good catch Luke. Sorry I did not catch it first.
src/Database/Model.php Outdated Show resolved Hide resolved
@tschallacka
Copy link

It seems that this has been denied for issues by the laravel repo several times
laravel/framework#22249
laravel/framework#22250
laravel/framework#23375

You should also use array_key_exists($key, $model->attributesToArray()) as per laravel/framework#22250 (comment) to prevent skipping over the attributes that are retrieveable via getter.

@LukeTowers
Copy link
Contributor

@firemankurt if you could make the changes suggested by @tschallacka that would be appreciated. I don't mind being better than Laravel by adding this into October :)
Will require documentation though.

Use "attributesToArray()" instead of just "attributes".
Made PSR-4 compliant
@LukeTowers
Copy link
Contributor

@firemankurt could you add this to the documentation in the Models section?

@LukeTowers
Copy link
Contributor

@firemankurt are you still interested in this?

@firemankurt
Copy link
Contributor Author

firemankurt commented May 26, 2020 via email

@LukeTowers
Copy link
Contributor

@firemankurt could you add this to the documentation in the Models section?

@github-actions
Copy link

This pull request will be closed and archived in 3 days, as there has been no activity in the last 60 days.
If this is still being worked on, please respond and we will re-open this pull request.
If this pull request is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.

@firemankurt
Copy link
Contributor Author

Hey Luke,
I'm still hoping this will be added to October.
Thanks

@bennothommo
Copy link
Contributor

@firemankurt we're still waiting on your documentation update for the Models docs. Did you happen to have a look at that?

@github-actions
Copy link

This pull request will be closed and archived in 3 days, as there has been no activity in the last 60 days.
If this is still being worked on, please respond and we will re-open this pull request.
If this pull request is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

4 participants