2

actually i'm creating a component in which i'm facing problem to restrict a backend user from direct edit a record.

I Know When we Edit a record in joomla using "controller.edit" function it uses HoldEditId() function to store the current id in userstate passed using url

i'm using the value stored using HoldEditId () function in model to check if the id of url is the same stored in userState Variable To Load Correct record data

But HoldEditId not only hold the current value but also hold the last edited record id now the problem is how can i know which is currently stored id and which is last inserted id

so the solution is to remove the old values save using holdeditid function, when a user click on close or cancel button but how can i do that and if this is right or i'm doing very wrong there is an another simple way.?

1

This is how you should load your current record id from the state in your model class.

$pk = (!empty($pk)) ? $pk : (int) $this->getState($this->getName() . '.id');

If you want to get last edited value in your holdEditId function, you can use:

$recordId = array_pop($values)

But ideally you need not override holdEditId function at all. You need to pass the id in your url so that it will be automatically populated in your model.

This happens in populateState function of your model if it extends JModelAdmin. You can override populateState function in your model to populate other variables if you need.

  • Thank your sir for your kind attention but I'm still confused about it. this->getState($this->getName() . '.id') is work fine but I'm confused how it is working as holdeditid store the value in userstate variable which again uses session but we are accessing the id from state variable of model..? how, where and when joomla populate model state variable – Sumer Raj Chouhan May 12 '15 at 6:55
  • As I said, the state is populated in your model (populateState function). You don't need to write this function if your model is extending from JModelAdmin. $this->getName() will return simply the name of the model, for example MyComponentModelArticle is the model, then the name is article. The values are populated from request variables. So your url may have to contain id as parameter. – Nagarjun May 12 '15 at 6:59
  • Ok sir I got it but $this->getState($this->getName() . '.id') will return the id passed using url, not the id going to store in userstate varible when user click on the Record how can i get that value stored by holdeditid in userstate variable.???? yes we can can fetch values of userstate variable but there are already multiple id's stored. these are the id of records previously edited by user – Sumer Raj Chouhan May 12 '15 at 7:15
  • 1
    When a user click on a url, obviously that url will have the id the row he clicked. If you are submitting form on clicking the url, make sure you have a input hidden field and is populated with id of the row clicked using javascript. You cannot rely on list of values stored on session as you cannot identify which row user clicked. Always insist on id from url. – Nagarjun May 12 '15 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.