web browser doesn't show any data from cassandra connection in yii2

I am newbie in yii2.

I want to show cassnadra data with php in yii2.I tried with netbeans & cmd and it(code) worked.( my os is linux) my question is why it does not work in web browser.how to confique yii2 to connect to cassandra in yii2.

Does it related to web browser’s permision ???????

$cluster = Cassandra::cluster()->build();

$keyspace = ‘keyspace’;

$session = $cluster->connect($keyspace);

$statement = new Cassandra\SimpleStatement(

‘SELECT * FROM new.country_by_id’);

$future = $session->executeAsync($statement);

$result = $future->get();

foreach ($result as $row)

{

print  $row['country_name']."<br />";

}

it work in cmd but not work in web browser.

pls help

thank you

for testing you can put your code in an action with configuration and run it

for production use i suggest you create a component. I use cassandra to store transactions with yii2 application (ads network), I have wrapped duoshuo/php-cassandra in a custom component and which in turn allows me to configure it in config/web.php much like normal db component and then I can use it through out the app with duplicating the code.

here is a snippet from my app




// app/components/Cassandra.php

<?php


namespace app\components;

 

use Yii;

use yii\base\Component;

 

class Cassandra extends Component

{


    public $keyspace = 'default_keyspace';


    public $conn;


    public function init()

    {

        // init your php library here

        // which makes the connection and perhaps something else

        $this->conn = Your\Library\Connection();

    }


    public function query($cql)

    {

        return $this->conn($cql)

    }

}




// config/web.php


'components' => [

    // other components ...


    'cassandra' => [

        'class' => 'app\components\Cassandra',

        'keyspace' => 'transactions'

    ],


    // ...

]


// controller where i use cassandra


<?php


namespace app\controllers;


use Yii;

use \yii\web\Controller


class SiteController extends \yii\web\Controller

{

    public function actionIndex()

    {

       $transactios = Yii::$app->cassandra->query('SELECT * FROM transactions');


        return $this->render('index', [

            'transactions' => $transactions

        ]);

    } 

}




in common/config/main-local.php

‘cassandra’ = [

        'class' = '&#092;beliy&#092;cassandra&#092;Connection',


        'keyspace' = 'new',


        'servers' = ['127.0.0.1'],


    ]

i installed php-cassandra driver from github

and i dont know how to load cassandra class in php file or yii2 project.

php-cassandra has pretty good readme go through and follow it.

and put it in the init function also look at the example above


public function init()

    {

        // init your php library here

        // which makes the connection and perhaps something else

        $this->conn = Your\Library\Connection();

    }

this is my code:

require_once ‘php-cassandra/php-cassandra.php’;

$nodes = [‘127.0.0.1’, // simple way, hostname only

];

// Create a connection.

$connection = new Cassandra\Connection($nodes, ‘news’);

//Connect

try

{

&#036;connection-&gt;connect();

}

catch (Cassandra\Exception $e)

{

echo 'Caught exception: ',  &#036;e-&gt;getMessage(), &quot;&#092;n&quot;;


exit;//if connect failed it may be good idea not to continue

}

// Set consistency level for farther requests (default is CONSISTENCY_ONE)

//$connection->setConsistency(Request::CONSISTENCY_QUORUM);

// Run query synchronously.

try

{

&#036;q = &#036;connection-&gt;querySync('SELECT * FROM country_by_id');

// $response = $q->getResponse();

&#036;rows1 = &#036;q-&gt;fetchAll();


    


var_dump(&#036;rows1);

}

catch (Cassandra\Exception $e)

{

echo 'Caught exception: ',  &#036;e-&gt;getMessage(), &quot;&#092;n&quot;;

}

and it work.thanks

but

i have a question ???????????????????

in that code we have casssandra 2.1 but i want to use DataStax Enterprise PHP Driver (github.com/datastax/php-dse-driver)

and i dont know how to use of clone file.i think that is newst driver for cadssandra.what do you think?????????

would you help me to run the this driver.

datastax/php-driver is a native driver you might need to install some extensions and c libraries, which will take some time to configure it and get it running. I can certainly help you.

0- i used above exmple for php file and it work but in yii2 i tried for run that code but i failed.

confiq path:application/backend/common/main-local.php


 <?php

    return [

        'components' => [

          

            'cassandra' => [

                'class' => '\beliy\cassandra\Connection',

                'keyspace' => 'new',

                //'keyspace' => 'myDB',

                'servers' => ['127.0.0.1'],

            ],

            'cache' => [

              'class' => '\beliy\cassandra\Cache',

              'tableName' => 'cache', // dedault 'cache'

              'tablePrefix' => 'mega', // default ''

            ],

           

        ],

    ];



in controller i used the same code in yii2 project




    <?php


    namespace backend\controllers;




    use Yii;

    use yii\data\ArrayDataProvider;


    use yii\web\Controller;

    use yii\web\NotFoundHttpException;

    use yii\filters\VerbFilter;

    use yii\helpers\Json;




    require_once Yii::$app->basePath . '/library/PhCass_2-1/php-cassandra.php';


    /**

     * ProfileController implements the CRUD actions for Profile model.

     */

    class NewController extends Controller

    {

        /**

         * @inheritdoc

         */

        public function behaviors()

        {

            return [

                'verbs' => [

                    'class' => VerbFilter::className(),

                    'actions' => [

                        'delete' => ['POST'],

                    ],

                ],

            ];

        }




        

	        public function actionIndex()

        {

           return $this->redirect(['new/home']);

        }

	        public function actionCreate()

        {

			

		    $nodes = [

			    '127.0.0.1',		// simple way, hostname only

		    ];

		

		    // Create a connection.

		    $connection = new Cassandra\Connection($nodes, 'new');

		

		    //Connect

		    try

		    {

			    $connection->connect();

		    }

		    catch (Cassandra\Exception $e)

		    {

			    echo 'Caught exception: ',  $e->getMessage(), "\n";

			    exit;//if connect failed it may be good idea not to continue

		    }

		

		

		    // Set consistency level for farther requests (default is CONSISTENCY_ONE)

		    //$connection->setConsistency(Request::CONSISTENCY_QUORUM);

		

		    // Run query synchronously.

		    try

		    {

			    $q = $connection->querySync('SELECT * FROM country_by_id');

			

			

		       // $response = $q->getResponse();

		

		

			    $rows1 = $q->fetchAll();

			

			    foreach($rows1  as  $value )

			    {

				    var_dump($value['country_name']); 

			    }

		       

		    }

		    catch ( Cassandra\Exception $e )

		    {

			    echo 'Caught exception: ',  $e->getMessage(), "\n";

		    }

    //	    return $this->render( 'create' , [ 'Country' => $Country ] );

        }    

       

            

    }






second question:

1- I installed all datastax php-cassandra driver and c extension and i used it , my first question in this topic refered to this issue , my problem is it work in cmd and with netbeans but it does not show any data in my chrome or firefox.

2- i want to learn cassandra and use it in php but i could not. I know cassandra modeling and concept of that but the problem is something i said.

3- i want to use latest version of cassandra driver and newst code to connect to cassandra and use it in yii2.

4- if you know it pls help me.

how to use this site -> https://datastax.github.io/php-driver/features/datatypes/

to learn cassandra php and cassandra in yii2