Wednesday, 21 January 2015

[PHP]Conventional v/s Configurable

ravimanephpmysql

Types of MVC: Conventional v/s Configurable




Now a days all the applications are being created using the MVC architecture, as it a most powerful and successful way to develop the application.

In this article we will see comparison of different types of MVC architecture we have, which are as below:

    Convention Based MVC
    Configuration Based MVC


Types of MVC Architecture

Convention Based MVC


As the name suggest, in this type of MVC we need to follow some predefined conventions in order to have it in working mode. This conventions are different from frameworks to frameworks. There are numbers of frameworks out there which follows convention based approach. Some of those frameworks are as below:

    CakePHP
    Codeigniter
    YUI
    Kohana

In this article we will see convention approach of the Codeigniter, we will see what types of convention are be there for creating a new model in Codeigniter.

First convention is storing of file, we need to store files under specific directory in order to treat it as model file. In Codeigniter all model files are stored under application/models/ directory.

Second convention is giving a name to the files. In Codeigniter model file name must be all lowercase.

Third convention is class name convention. In Codeigniter class name is depends on model file name which we have created in above step. Class name must be same as file name except First character in Class name must be uppercase, also model class in Codeigniter must extends the base class which is CI_Model. Let’s take all above cases with example.

In this example we will create a new model (product) from scratch.

Very first you need to create your model file under application/models/, in this case it would be /application/models/product_model.php

Now we need to define a class in that file. Based on our file name class name would be Product_model which will extend the base class CI_Model. So basic code of this class would be same as below code block.
Copy
class Product_model extends CI_Model
{
   function __construct()
   {
       parent::__construct();
   }
}


Configuration Based MVC


Unlike Conventional based MVC, in Configuration based MVC we have full control over file name and class name we use. We can explicitly define which files to load for particular model and which class to find from that file.

Magento is the best example of Configuration based MVC architecture. In Magento each module have its own config.xml file. In this file all the configuration of that particular module, like which files to check, which class to load, etc.

    Magento is the best example of Configuration based MVC architecture.

In Magento if we want to use custom Model in our module then we will need to add some code in config.xml file to instruct Magento about the class name to search for. Please check below example for the same:
Copy
<models>
    <packagename>
         <class>Packagename_Modulename_Model</class>
   <packagename>
</packagename></packagename></models>

So these are my thoughts on Different types of MVC architecture we can have with PHP. Hey guys do you have any other approach to create an MVC architecture?

Sunday, 11 January 2015

What happens to cronjobs scheduled when the server is off?

ravimanephpmysql

Question:I have an Ubuntu Server 12 which isn't on all day. In the /etc/crontab, I see that cron.daily, cron.weekly and cron.monthly are scheduled at times the server is off, usually.What happens with those tasks? Will they just be skipped, or runned as soon as the server is on again.



ANSWER:
cron skips jobs if it isn't running at the time that they are scheduled.

However, if you have anacron installed, then daily, weekly and monthly jobs are run "at the specified intervals as closely as machine uptime permits".

In /etc/crontab, you should see references to anacron. This logic uses anacron for regular jobs, but only if anacron is installed. anacron is installed by default on Ubuntu Desktop, but not on Server, since servers are generally expected to be on 24/7. But you can install anacron on Ubuntu Server if you want the same behaviour there, and it should work as expected.

What is Cron Job In Php and it's basic types. and what is server is off at cron job time

ravimanephpmysql
This depends on which cron scheduler you use. The basic, vanilla cron daemon will not run tasks that were missed due to system downtime. However, there are other cron schedulers specifically designed for this situation which will do this for you. The two most common examples are anacron and fcron.

Saturday, 10 January 2015

[PHP] Create MySQL Trigger in PHP

phpmysql
IntroductionWhen we want to execute a specific task at a scheduled time, MySQL provides special functionality called triggers. Triggers are basically named blocks of code that are executed, or fired, automatically when a specified type of SQL statement is executed. Triggers can be executed before after an INSERT, UPDATE or DELETE statement is executed on the table.

Syntax



CREATE TRIGGER trigger_Name
{Before|After}{Insert|Update|Delete}ON table_name
For  EACH  ROW
sql_block

For ExampleUsing the following query, you can better understand what a trigger is and how to use a trigger to execute a specific task at a scheduled time.

QueryThe following query creates a  trigger named "MysqlTrigger", this trigger is associated with the name emp table and is fired before an update. When you try to update a name with lower case, it will automatically convert it to UPPER case .
"CREATE TRIGGER MysqlTrigger BEFORE UPDATE ON "emp" FOR EACH ROW SET NEW.name=UPPER(NEW.name);"

The following is an Update statement that fires the trigger:

UPDATE emp SET name='ram' where id=2


The following is a select statement that shows the new row:

Select * from emp where id=2





Example of Creating a trigger in PHP



In this example a simple "CREATE TRIGGER MysqlTrigger BEFORE UPDATE ON emp FOR EACH ROW SET NEW.name=UPPER(NEW.name);" query creates a trigger that will be useful when for updating an employee's name based on their id (in other words emp id). Suppose you write the query "UPDATE emp SET name='ram' where id=2", when this query fires, the name corresponding to id 2 is updated. Suppose that before the update, the name corresponding to id 2 is "vinod", then this query changes "vinod" to "ram". In other words, the trigger that was previously created by you, changes "ram" to "RAM".

<?php

$con=mysql_connect("localhost","root","");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}

mysql_select_db("sharad", $con);

print "<h2>MySQL: Simple Select statement</h2>";

$result = mysql_query("select * from emp");

echo "<table border='1'>

<tr>

<th>EmpId</th>

<th>Firstname</th>

<th>Salary</th>

</tr>";

while($row = mysql_fetch_array($result))

{

echo "<tr>";

echo "<td>" . $row['id'] . "</td>";

echo "<td>" . $row['name'] . "</td>";

echo "<td>" . $row['salary'] . "</td>";

echo "</tr>";

}

echo "</table>";

print "<h2>CREATE MySQL Trigger In PHP</h2>";

$sql = "CREATE TRIGGER MysqlTrigger BEFORE UPDATE ON emp FOR EACH ROW SET NEW.name=UPPER(NEW.name);";

mysql_query($sql,$con);

print "<h2>MySQL: Update Statement</h2>";

$qry = mysql_query("UPDATE emp SET name='ram' where id=2");

echo "Table has been updated.";

mysql_query($qry,$con);

print "<h2>MySQL: Effect of Trigger</h2>";

<?php

$con=mysql_connect("localhost","root","");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}$result = mysql_query("select * from emp");

echo "<table border='1'>

<tr>

<th>EmpId</th>

<th>Firstname</th>

<th>Salary</th>

<?php

$con=mysql_connect("localhost","root","");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}

mysql_select_db("sharad", $con);

print "<h2>MySQL: Simple Select statement</h2>";

$result = mysql_query("select * from emp");

echo "<table border='1'>

<tr>

<th>EmpId</th>

<th>Firstname</th>

<th>Salary</th>

</tr>";

while($row = mysql_fetch_array($result))

{

echo "<tr>";

echo "<td>" . $row['id'] . "</td>";

echo "<td>" . $row['name'] . "</td>";

echo "<td>" . $row['salary'] . "</td>";

echo "</tr>";

}

echo "</table>";

print "<h2>CREATE MySQL Trigger In PHP</h2>";

$sql = "CREATE TRIGGER MysqlTrigger BEFORE UPDATE ON emp FOR EACH ROW SET NEW.name=UPPER(NEW.name);";
</tr>";

while($row = mysql_fetch_array($result))

{

echo "<tr>";

echo "<td>" . $row['id'] . "</td>";

echo "<td>" . $row['name'] . "</td>";

echo "<td>" . $row['salary'] . "</td>";

echo "</tr>";

}

echo "</table>";

m<?php

$con=mysql_connect("localhost","root","");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}ysql_close($con);



<?php

$con=mysql_connect("localhost","root","");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}?>Note: You can not create a trigger on "system tables".

Following one also better example . make trigger before insert qurey


$con=mysql_connect("localhost", "root", "");
$connect=mysql_select_db("ci",$con);
if(isset($connect))
{
    echo 'successfully connected';
  
   
<?php

$con=mysql_connect("localhost","root","");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}
$
<?php

$con=mysql_connect("localhost","root","");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}
s
<?php
<?php

$con=mysql_connect("localhost","root","");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}
$con=mysql_connect("localhost","root","");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}
ql="CREATE TRIGGER captial BEFORE INSERT ON details FOR EACH ROW SET NEW.lastname=upper(NEW.lastname)";
    mysql_query($sql);
  
  
    $sql1="INSERT INTO details(lastname) VALUES('desai')";
    mysql_query($sql1);
      
}  


Force git to add dotfiles to repository

how would you add files which are started with dot(hidden files) in git repo? when i am new at git , I also faced the same problem,  as...