Jump to content

Using a if condition in a model


Iluvatar+

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'))
			)
			)
		 );


}
?> 

Link to comment
Share on other sites

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'))
			)
			)
		 );
		}

}
?> 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.