Iluvatar+ Posted March 7, 2012 Share Posted March 7, 2012 If there any way of using if conditions with in a class but outside any actions with in that class. I need to say if current controller action is in use then use then set these vars else set another set of vars. Keep in mind i am using cakephp framework. Quote Link to comment https://forums.phpfreaks.com/topic/258453-using-a-if-condition-in-a-model/ Share on other sites More sharing options...
trq Posted March 7, 2012 Share Posted March 7, 2012 We need to see relevant code. Quote Link to comment https://forums.phpfreaks.com/topic/258453-using-a-if-condition-in-a-model/#findComment-1324809 Share on other sites More sharing options...
Iluvatar+ Posted March 7, 2012 Author Share Posted March 7, 2012 It's clearly self-explanatory Quote Link to comment https://forums.phpfreaks.com/topic/258453-using-a-if-condition-in-a-model/#findComment-1324828 Share on other sites More sharing options...
trq Posted March 7, 2012 Share Posted March 7, 2012 Clearly. Quote Link to comment https://forums.phpfreaks.com/topic/258453-using-a-if-condition-in-a-model/#findComment-1324834 Share on other sites More sharing options...
ManiacDan Posted March 7, 2012 Share Posted March 7, 2012 If you believe that question is self-explanatory then your answer is "err...no...?" If you're asking "is it possible to have conditional code inside a class but outside the functions in that class" then the PHP interpreter itself could have given you that answer: trying to do that produces a fatal error. Quote Link to comment https://forums.phpfreaks.com/topic/258453-using-a-if-condition-in-a-model/#findComment-1324839 Share on other sites More sharing options...
Iluvatar+ Posted March 7, 2012 Author Share Posted March 7, 2012 I apologize if i sounded rude, it wasn’t my intention. I have a user m/v/c, in my controller i currently have four action (login, logout, add, edit). Within the edit section i have an image uploaded component in use for the user avatar, however in the add user action i don’t want to have the same component in the form i just want to add content to specific fields in the record. In my model i have numerous parameters set in regards to the upload component, but when i am using the add user action the image uploader parameters seem to be stopping my action from inserting a new record. What i need is a condition that will allow me to run them parameters when necessary (with in the action that uses it - edit action). here is my code anyway... <?php class User extends AppModel { var $name = 'User'; # This needs to be used for the image upload functionality. var $actsAs = array('Media.Transfer' => array('trustClient' => false, 'transferDirectory' => MEDIA_TRANSFER, 'createDirectory' => true, 'alternativeFile' => 100 ), 'Media.Coupler', 'Media.Generator' => array( 'baseDirectory' => MEDIA_TRANSFER, 'filterDirectory' => MEDIA_USER, 'createDirectory' => true, ), ); // file validation params (which only allowed jpeg and png to be uploaded) var $validate = array( 'file' => array( 'mimeType' => array( 'rule' => array('checkMimeType', false, array( 'image/jpeg', 'image/png')) ) ) ); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/258453-using-a-if-condition-in-a-model/#findComment-1324844 Share on other sites More sharing options...
trq Posted March 7, 2012 Share Posted March 7, 2012 I don't understand the issue. You can use a simple if statement. Quote Link to comment https://forums.phpfreaks.com/topic/258453-using-a-if-condition-in-a-model/#findComment-1324976 Share on other sites More sharing options...
Iluvatar+ Posted March 8, 2012 Author Share Posted March 8, 2012 I get "Parse error: syntax error, unexpected T_IF, expecting T_FUNCTION in C:\wamp\www\NEW\cms\app\models\user.php on line 20" <?php class User extends AppModel { var $name = 'User'; var $validate = array( 'login' => array( 'rule' => array('_isUnique', 'login'), 'message' => 'Login name already taken.' ) ); /*---------------------------------------------------------------------------------------------------------------------- # Add user premisons model var $belongsTo = array('Userpagepremission' => array('className' => 'Userpagepremission', 'foreignKey' => false, 'conditions'=> array('Userpagepremission.user_id = User.id') ) ); -----------------------------------------------------------------------------------------------------------------------*/ if($this->action=='editprofile') { # This needs to be used for the image upload functionality. var $actsAs = array('Media.Transfer' => array('trustClient' => false, 'transferDirectory' => MEDIA_TRANSFER, 'createDirectory' => true, 'alternativeFile' => 100 ), 'Media.Coupler', 'Media.Generator' => array( 'baseDirectory' => MEDIA_TRANSFER, 'filterDirectory' => MEDIA_USER, 'createDirectory' => true, ), ); // file validation params (which only allowed jpeg and png to be uploaded) var $validate = array( 'file' => array( 'mimeType' => array( 'rule' => array('checkMimeType', false, array( 'image/jpeg', 'image/png')) ) ) ); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/258453-using-a-if-condition-in-a-model/#findComment-1325143 Share on other sites More sharing options...
trq Posted March 8, 2012 Share Posted March 8, 2012 Sorry but your code is unreadable with that kind of indentation. Anyway, you cannot have if statements in the body of a class like that. You'll need to move all of your logic into your __construct() if your plan is to conditionally assign values to an array when the object is created. Quote Link to comment https://forums.phpfreaks.com/topic/258453-using-a-if-condition-in-a-model/#findComment-1325149 Share on other sites More sharing options...
Iluvatar+ Posted March 8, 2012 Author Share Posted March 8, 2012 as in create a function called _construct? Quote Link to comment https://forums.phpfreaks.com/topic/258453-using-a-if-condition-in-a-model/#findComment-1325154 Share on other sites More sharing options...
trq Posted March 8, 2012 Share Posted March 8, 2012 as in create a function called _construct? __construct(). You should probably become familiar with how classes and objects work in PHP before using any framework. http://php.net/oop5 Quote Link to comment https://forums.phpfreaks.com/topic/258453-using-a-if-condition-in-a-model/#findComment-1325185 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.